|
|
@@ -9,6 +9,8 @@ use App\Model\EmployeeDepartPermission;
|
|
|
use App\Model\EmployeeIndex;
|
|
|
use App\Model\Freight;
|
|
|
use App\Model\FreightFee;
|
|
|
+use App\Model\ItemReport;
|
|
|
+use App\Model\ItemReportRoad;
|
|
|
use App\Model\Product;
|
|
|
use App\Model\RevenueCost;
|
|
|
use App\Model\RevenueCostTotal;
|
|
|
@@ -52,6 +54,8 @@ class TPlusServerService extends Service
|
|
|
private $table = "tmp_revenue_cost_data";
|
|
|
private $table_2 = "tmp_salary_employee";
|
|
|
private $table_3 = "tmp_freight_fee";
|
|
|
+ private $table_4 = "tmp_item_road";
|
|
|
+ private $table_5 = "tmp_item_cost";
|
|
|
|
|
|
/**
|
|
|
* 同步人员部门
|
|
|
@@ -173,7 +177,7 @@ class TPlusServerService extends Service
|
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
|
|
|
list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
|
|
|
if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
|
|
|
- list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
|
|
|
+ list($bool, $bool_msg) = $this->isWithinMonths($start_time, $end_time);
|
|
|
if(! $bool) return [false, $bool_msg];
|
|
|
$data['start_timeStamp'] = $start_time;
|
|
|
$data['end_timeStamp'] = $end_time;
|
|
|
@@ -796,13 +800,20 @@ class TPlusServerService extends Service
|
|
|
if (Schema::hasTable($this->table_2)) DB::table($this->table_2)->truncate();
|
|
|
}elseif ($type == 3){
|
|
|
if (Schema::hasTable($this->table_3)) DB::table($this->table_3)->truncate();
|
|
|
+ }elseif ($type == 4){
|
|
|
+ if (Schema::hasTable($this->table_4)) DB::table($this->table_4)->truncate();
|
|
|
+ if (Schema::hasTable($this->table_5)) DB::table($this->table_5)->truncate();
|
|
|
}
|
|
|
+// elseif ($type == 5){
|
|
|
+// if (Schema::hasTable($this->table_5)) DB::table($this->table_5)->truncate();
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
public function delTableKey($type = 1){
|
|
|
$key = $this->table;
|
|
|
if($type == 2) $key = $this->table_2;
|
|
|
if($type == 3) $key = $this->table_3;
|
|
|
+ if($type == 4) $key = $this->table_4;
|
|
|
$this->dellimitingSendRequest($key);
|
|
|
}
|
|
|
|
|
|
@@ -810,7 +821,7 @@ class TPlusServerService extends Service
|
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
|
|
|
list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
|
|
|
if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
|
|
|
- list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
|
|
|
+ list($bool, $bool_msg) = $this->isWithinMonths($start_time, $end_time);
|
|
|
if(! $bool) return [false, $bool_msg];
|
|
|
$data['start_timeStamp'] = $start_time;
|
|
|
$data['end_timeStamp'] = $end_time;
|
|
|
@@ -1039,7 +1050,7 @@ class TPlusServerService extends Service
|
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
|
|
|
list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
|
|
|
if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
|
|
|
- list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
|
|
|
+ list($bool, $bool_msg) = $this->isWithinMonths($start_time, $end_time);
|
|
|
if(! $bool) return [false, $bool_msg];
|
|
|
$data['start_timeStamp'] = $start_time;
|
|
|
$data['end_timeStamp'] = $end_time;
|
|
|
@@ -1396,4 +1407,1165 @@ class TPlusServerService extends Service
|
|
|
|
|
|
return [true, ''];
|
|
|
}
|
|
|
+
|
|
|
+ public function synItemRoad($data, $user){
|
|
|
+ if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
|
|
|
+ $start_time = strtotime($data['crt_time'][0] . "-01");
|
|
|
+ $end_time = strtotime(date('Y-m-t', strtotime($data['crt_time'][1] . '-01')) . ' 00:00:00');
|
|
|
+ list($bool, $bool_msg) = $this->isWithinMonths($start_time, $end_time);
|
|
|
+ if(! $bool) return [false, $bool_msg];
|
|
|
+
|
|
|
+ $data['start_timeStamp'] = $start_time;
|
|
|
+ $data['end_timeStamp'] = $end_time;
|
|
|
+ $start = date('Y-m-d H:i:s.000', $start_time);
|
|
|
+ $end = date('Y-m-d H:i:s.000', $end_time);
|
|
|
+ $data['start_time'] = $start;
|
|
|
+ $data['end_time'] = $end;
|
|
|
+ $data['operation_time'] = time();
|
|
|
+ $data['type'] = 7;
|
|
|
+
|
|
|
+// list($status, $msg) = $this->synItemRoadMy($data, $user);dd($status, $msg);
|
|
|
+
|
|
|
+ list($status,$msg) = $this->limitingSendRequest($this->table_4);
|
|
|
+ if(! $status) return [false, '统计同步正在后台运行,请稍后'];
|
|
|
+
|
|
|
+ //队列
|
|
|
+ ProcessDataJob::dispatch($data, $user, 4)->onQueue(RevenueCost::job4);
|
|
|
+
|
|
|
+ return [true, '统计同步已进入后台任务'];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createTmpTable4(){
|
|
|
+ $table = $this->table_4;
|
|
|
+ if (! Schema::hasTable($table)) {
|
|
|
+ Schema::create($table, function (Blueprint $table) {
|
|
|
+ $table->bigIncrements('id');
|
|
|
+
|
|
|
+ // 订单基础信息
|
|
|
+ $table->bigInteger('order_id')->default(0)->comment('收款单ID(rp.ID)');
|
|
|
+ $table->string('order_number')->default('')->comment('收款单编号(rp.code)');
|
|
|
+ $table->integer('order_time')->default(0)->comment('单据时间(时间戳)');
|
|
|
+ $table->integer('order_state')->default(0)->comment('订单状态');
|
|
|
+ $table->integer('order_type')->default(0)->comment('订单分类标识');
|
|
|
+
|
|
|
+ // 渠道/分销相关
|
|
|
+ $table->string('channel_finance')->default('')->comment('渠道财务(rp.pubuserdefnvc11)');
|
|
|
+ $table->string('channel_details')->default('')->comment('渠道明细(rp.pubuserdefnvc12)');
|
|
|
+ $table->string('channel_details_fp')->default('')->comment('发票渠道明细(si.pubuserdefnvc12)');
|
|
|
+
|
|
|
+ // 客户信息
|
|
|
+ $table->string('customer_code')->default('')->comment('结算客户编码');
|
|
|
+ $table->string('customer_title')->default('')->comment('结算客户名称');
|
|
|
+
|
|
|
+ // 人员信息
|
|
|
+ $table->bigInteger('employee_id_1')->default(0)->comment('业务员ID');
|
|
|
+ $table->string('employee_id_1_title')->default('')->comment('业务员名称');
|
|
|
+ $table->bigInteger('employee_id_2')->default(0)->comment('上级管理人员ID');
|
|
|
+ $table->string('employee_id_2_title')->default('')->comment('上级管理人员名称');
|
|
|
+
|
|
|
+ // 产品信息 (仅限 idvouchertype = 20)
|
|
|
+ $table->string('product_code')->default('')->comment('产品编码');
|
|
|
+ $table->string('product_title')->default('')->comment('产品名称');
|
|
|
+ $table->decimal('quantity', 14, 3)->default(0)->comment('发票数量');
|
|
|
+
|
|
|
+ // 收入/价格相关
|
|
|
+ $table->decimal('price_1', 14, 4)->default(0)->comment('含税单价(taxPrice)');
|
|
|
+ $table->decimal('price_1_total', 14, 2)->default(0)->comment('含税金额(taxAmount)');
|
|
|
+ $table->decimal('payment_amount', 14, 2)->default(0)->comment('明细行的收款金额');
|
|
|
+
|
|
|
+ // 成本相关 (计算得出)
|
|
|
+ $table->decimal('price_4', 14, 4)->default(0)->comment('业务成本单价');
|
|
|
+ $table->decimal('price_4_total', 14, 2)->default(0)->comment('业务成本总额');
|
|
|
+
|
|
|
+ // 关联追溯字段
|
|
|
+ $table->bigInteger('id_detail')->default(0)->comment('收款明细ID(rp_b.ID)');
|
|
|
+ $table->bigInteger('id_detail_upstream')->default(0)->comment('上游单据明细ID(voucherDetailID)');
|
|
|
+ $table->string('order_number_upstream')->default('')->comment('上游单据编号(voucherCode)');
|
|
|
+ $table->integer('voucher_type')->default(0)->comment('单据类型(idvouchertype)');
|
|
|
+ $table->string('is_kh')->default('0')->comment('是否计入考核(sd.priuserdefnvc4)');
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createTmpTable5(){
|
|
|
+ $table = $this->table_5;
|
|
|
+ if (! Schema::hasTable($table)) {
|
|
|
+ Schema::create($table, function (Blueprint $table) {
|
|
|
+ $table->bigIncrements('id');
|
|
|
+
|
|
|
+ // 基础信息
|
|
|
+ $table->bigInteger('order_id')->default(0)->comment('费用单ID(cs.ID)');
|
|
|
+ $table->string('order_number')->default('')->comment('费用单编号(cs.code)');
|
|
|
+ $table->integer('order_time')->default(0)->comment('单据时间(时间戳)');
|
|
|
+ $table->integer('order_type')->default(0)->comment('订单分类标识(1 费用单 2 其他出库单)');
|
|
|
+ $table->string('ck_type')->default("")->comment('出库单出库类别');
|
|
|
+ $table->integer('employee_id_1')->default(0)->comment('业务员id');
|
|
|
+
|
|
|
+ // 维度信息
|
|
|
+ $table->string('channel_details')->default('')->comment('渠道明细(cs.pubuserdefnvc12)');
|
|
|
+ $table->string('fy_code')->default('')->comment('费用项目编码(ae.code)');
|
|
|
+
|
|
|
+ // 金额信息
|
|
|
+ $table->decimal('taxamount', 14, 2)->default(0)->comment('含税金额(cs_b.taxamount)');
|
|
|
+
|
|
|
+ // 追溯信息
|
|
|
+ $table->bigInteger('id_detail')->default(0)->comment('费用明细行ID(cs_b.ID)');
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function synItemRoadMy($data, $user){
|
|
|
+ //收入
|
|
|
+ list($status, $msg) = $this->synReceipt($data,$user);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ //成本
|
|
|
+ list($status, $msg) = $this->synSaleCost($data,$user);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ //汇总项目报表
|
|
|
+ list($status, $msg) = $this->saveResult($data,$user);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ //清理临时表 如果内容不为空
|
|
|
+ $this->clearTmpTable(4);
|
|
|
+ //释放redis
|
|
|
+ $this->delTableKey(4);
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function synReceipt($data, $user){
|
|
|
+ //创建临时表 如果不存在
|
|
|
+ $this->createTmpTable4();
|
|
|
+ //清理临时表 如果内容不为空
|
|
|
+ $this->clearTmpTable(4);
|
|
|
+
|
|
|
+ try{
|
|
|
+ $table = $this->table_4;
|
|
|
+ $limit = 500;
|
|
|
+ $lastId = 0;
|
|
|
+
|
|
|
+ do {
|
|
|
+ $rows = $this->databaseService->table('ARAP_ReceivePayment_b as rp_b')
|
|
|
+ ->join('ARAP_ReceivePayment as rp', 'rp_b.idArapReceivePaymentDTO', '=', 'rp.ID')
|
|
|
+ // 发票子表关联(仅限 idvouchertype = 20)
|
|
|
+ ->leftJoin('SA_SaleInvoice_b as si_b', function ($join) {
|
|
|
+ $join->on('rp_b.voucherDetailID', '=', 'si_b.ID')
|
|
|
+ ->where('rp_b.idvouchertype', '=', 20);
|
|
|
+ })
|
|
|
+ ->leftJoin('SA_SaleInvoice as si', function ($join) {
|
|
|
+ $join->on('si_b.idSaleInvoiceDTO', '=', 'si.ID')
|
|
|
+ ->where('rp_b.idvouchertype', '=', 20);
|
|
|
+ })
|
|
|
+ ->leftJoin('SA_SaleDelivery_b as sd_b', function ($join) {
|
|
|
+ $join->on('si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
|
|
|
+ ->where('rp_b.idvouchertype', '=', 20);
|
|
|
+ })
|
|
|
+ ->leftJoin('SA_SaleDelivery as sd', function ($join) {
|
|
|
+ $join->on('sd_b.idSaleDeliveryDTO', '=', 'sd.ID')
|
|
|
+ ->where('rp_b.idvouchertype', '=', 20);
|
|
|
+ })
|
|
|
+ ->leftJoin('AA_Inventory as it', 'sd_b.idinventory', '=', 'it.ID')
|
|
|
+ ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID') //结算客户
|
|
|
+ ->leftJoin('AA_Person as ps', 'rp.idperson', '=', 'ps.ID')
|
|
|
+ ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID') // 结算客户的上级管理人员
|
|
|
+ ->where('rp.voucherdate','>=',$data['start_time'])
|
|
|
+ ->where('rp.voucherdate','<=',$data['end_time'])
|
|
|
+// ->where('priuserdefnvc4','=', 1) //.. 不计入考核 todo
|
|
|
+ ->where('rp.isReceiveFlag','=', 1)
|
|
|
+ ->where('rp_b.ID', '>', $lastId)
|
|
|
+ ->orderBy('rp_b.ID')
|
|
|
+ ->limit($limit)
|
|
|
+ ->selectRaw("
|
|
|
+ COALESCE(rp.ID, 0) as order_id,
|
|
|
+ COALESCE(rp.code, '') as order_number,
|
|
|
+ rp.voucherdate as order_time,
|
|
|
+ rp.voucherstate as order_state,
|
|
|
+ COALESCE(rp.pubuserdefnvc11, '') as channel_finance,
|
|
|
+ COALESCE(rp.pubuserdefnvc12, '') as channel_details,
|
|
|
+ COALESCE(si.pubuserdefnvc12, '') as channel_details_fp,
|
|
|
+ COALESCE(pn.code, '') as customer_code,
|
|
|
+ COALESCE(pn.name, '') as customer_title,
|
|
|
+ CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.code, '') ELSE '' END as product_code,
|
|
|
+ CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.name, '') ELSE '' END as product_title,
|
|
|
+ COALESCE(rp.idperson, 0) as employee_id_1,
|
|
|
+ COALESCE(ps.name, '') as employee_id_1_title,
|
|
|
+ COALESCE(pn.idsaleman, 0) as employee_id_2,
|
|
|
+ COALESCE(ps2.name, '') as employee_id_2_title,
|
|
|
+ CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.quantity, 0) ELSE 0 END as quantity,
|
|
|
+ CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxPrice, 0) ELSE 0 END as price_1,
|
|
|
+ CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxAmount, 0) ELSE 0 END as price_1_total,
|
|
|
+ COALESCE(rp_b.origCurrentAmount, 0) as payment_amount,
|
|
|
+ COALESCE(rp_b.ID, 0) as id_detail,
|
|
|
+ COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
|
|
|
+ COALESCE(rp_b.voucherCode, '') as order_number_upstream,
|
|
|
+ rp_b.idvouchertype as voucher_type,
|
|
|
+ CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(sd.priuserdefnvc4, '') ELSE '' END as is_kh
|
|
|
+ ")
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($rows->isEmpty()) break;
|
|
|
+
|
|
|
+ $dataArray = Collect($rows)->map(function ($object) {
|
|
|
+ return (array)$object;
|
|
|
+ })->toArray();
|
|
|
+
|
|
|
+ //存货档案业务成本
|
|
|
+ $product_map = Product::where('del_time',0)
|
|
|
+ ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
|
|
|
+ ->pluck('business_cost','code')
|
|
|
+ ->all();
|
|
|
+
|
|
|
+ //组织数据
|
|
|
+ foreach ($dataArray as $key => $value){
|
|
|
+ $p_tmp = $product_map[$value['product_code']] ?? 0;
|
|
|
+ $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
|
|
|
+ $dataArray[$key]['order_time'] = strtotime($value['order_time']);
|
|
|
+ //业务成本单价和金额
|
|
|
+ $business_cost = $p_tmp ?? 0;
|
|
|
+ $dataArray[$key]['price_4'] = $business_cost;
|
|
|
+ $price_4_total = bcmul($business_cost, $value['quantity'],2);
|
|
|
+ $dataArray[$key]['price_4_total'] = $price_4_total;
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::table($table)->insert($dataArray);
|
|
|
+
|
|
|
+ // 更新 lastId 继续下一批
|
|
|
+ $lastId = end($dataArray)['id_detail'] ?? $lastId;
|
|
|
+
|
|
|
+ } while (count($rows) === $limit);
|
|
|
+
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ return [false, "收入同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function synSaleCost($data, $user){
|
|
|
+ //创建临时表 如果不存在
|
|
|
+ $this->createTmpTable5();
|
|
|
+ //清理临时表 如果内容不为空
|
|
|
+// $this->clearTmpTable(5);
|
|
|
+
|
|
|
+ try{
|
|
|
+ $table = $this->table_5;
|
|
|
+ $limit = 500;
|
|
|
+ $lastId = 0;
|
|
|
+
|
|
|
+ do {
|
|
|
+ $rows = $this->databaseService->table('CS_ExpenseVoucher_b as cs_b')
|
|
|
+ ->join('CS_ExpenseVoucher as cs', 'cs_b.idExpenseVoucherDTO', '=', 'cs.ID')
|
|
|
+ ->leftJoin('AA_ExpenseItem as ae', 'ae.id', '=', 'cs_b.idexpenseitem')
|
|
|
+ ->where('cs.voucherdate','>=',$data['start_time'])
|
|
|
+ ->where('cs.voucherdate','<=',$data['end_time'])
|
|
|
+ ->where('cs_b.ID', '>', $lastId)
|
|
|
+ ->orderBy('cs_b.ID')
|
|
|
+ ->limit($limit)
|
|
|
+ ->selectRaw("
|
|
|
+ 1 as order_type,
|
|
|
+ COALESCE(cs.ID, 0) as order_id,
|
|
|
+ COALESCE(cs.code, '') as order_number,
|
|
|
+ cs.voucherdate as order_time,
|
|
|
+ cs.idclerk as employee_id_1,
|
|
|
+ COALESCE(cs.pubuserdefnvc12, '') as channel_details,
|
|
|
+ COALESCE(ae.code, '') as fy_code,
|
|
|
+ COALESCE(cs_b.taxamount, 0) as taxamount,
|
|
|
+ COALESCE(cs_b.ID, 0) as id_detail
|
|
|
+ ")
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($rows->isEmpty()) break;
|
|
|
+
|
|
|
+ $dataArray = Collect($rows)->map(function ($object) {
|
|
|
+ return (array)$object;
|
|
|
+ })->toArray();
|
|
|
+
|
|
|
+ foreach ($dataArray as $key => $value){
|
|
|
+ $dataArray[$key]['order_time'] = strtotime($value['order_time']);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::table($table)->insert($dataArray);
|
|
|
+
|
|
|
+ // 更新 lastId 继续下一批
|
|
|
+ $lastId = end($dataArray)['id_detail'] ?? $lastId;
|
|
|
+
|
|
|
+ } while (count($rows) === $limit);
|
|
|
+
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ return [false, "费用同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
|
|
|
+ }
|
|
|
+
|
|
|
+ try{
|
|
|
+ $table = $this->table_5;
|
|
|
+ $limit = 500;
|
|
|
+ $lastId = 0;
|
|
|
+
|
|
|
+ do {
|
|
|
+ $rows = $this->databaseService->table('ST_RDRecord_b as st_b')
|
|
|
+ ->join('ST_RDRecord as st', 'st_b.idRDRecordDTO', '=', 'st.ID')
|
|
|
+ ->where('st.voucherdate','>=',$data['start_time'])
|
|
|
+ ->where('st.voucherdate','<=',$data['end_time'])
|
|
|
+ ->where('st.idvouchertype', 30)
|
|
|
+ ->whereIn('st.idbusitype',[20048,60,20049]) //20049 客情出库 60 赠品出库 20048 员工福利
|
|
|
+ ->where('st_b.ID', '>', $lastId)
|
|
|
+ ->orderBy('st_b.ID')
|
|
|
+ ->limit($limit)
|
|
|
+ ->selectRaw("
|
|
|
+ 2 as order_type,
|
|
|
+ COALESCE(st.ID, 0) as order_id,
|
|
|
+ COALESCE(st.code, '') as order_number,
|
|
|
+ st.voucherdate as order_time,
|
|
|
+ st.idclerk as employee_id_1,
|
|
|
+ COALESCE(st.pubuserdefnvc12, '') as channel_details,
|
|
|
+ COALESCE(st_b.amount, 0) as taxamount,
|
|
|
+ COALESCE(st_b.ID, 0) as id_detail,
|
|
|
+ st.idbusitype as ck_type
|
|
|
+ ")->get();
|
|
|
+
|
|
|
+ if ($rows->isEmpty()) break;
|
|
|
+
|
|
|
+ $dataArray = Collect($rows)->map(function ($object) {
|
|
|
+ return (array)$object;
|
|
|
+ })->toArray();
|
|
|
+
|
|
|
+ foreach ($dataArray as $key => $value){
|
|
|
+ $dataArray[$key]['order_time'] = strtotime($value['order_time']);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::table($table)->insert($dataArray);
|
|
|
+
|
|
|
+ // 更新 lastId 继续下一批
|
|
|
+ $lastId = end($dataArray)['id_detail'] ?? $lastId;
|
|
|
+
|
|
|
+ } while (count($rows) === $limit);
|
|
|
+
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ return [false, "其他出库同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function saveResult($data, $user){
|
|
|
+ // 初始化汇总数组------报表一
|
|
|
+ $paymentSummary = [];
|
|
|
+ $costSummary = [];
|
|
|
+ $costSummary2 = [];
|
|
|
+
|
|
|
+ //报表二
|
|
|
+ $paymentSummary_road = [];
|
|
|
+ $costSummary_road = [];
|
|
|
+ $costSummary2_road = [];
|
|
|
+
|
|
|
+ $map = [
|
|
|
+ 2 => [
|
|
|
+ 'type' => 1,
|
|
|
+ 'channel_details' => ['通路'],
|
|
|
+ ],
|
|
|
+ 3 => [
|
|
|
+ 'type' => 2,
|
|
|
+ 'channel_details' => ['通路'],
|
|
|
+ ],
|
|
|
+ 23 => [
|
|
|
+ 'type' => 3,
|
|
|
+ 'channel_details' => ['通路'],
|
|
|
+ ],
|
|
|
+ 7 => [
|
|
|
+ 'type' => 4,
|
|
|
+ 'channel_details' => ['通路','大卖场'],
|
|
|
+ ],
|
|
|
+ 8 => [
|
|
|
+ 'type' => 4,
|
|
|
+ 'channel_details' => ['通路','大卖场'],
|
|
|
+ ],
|
|
|
+ 48 => [
|
|
|
+ 'type' => 4,
|
|
|
+ 'channel_details' => ['通路','大卖场'],
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ try{
|
|
|
+ $limit = 500;
|
|
|
+ $lastId = 0;
|
|
|
+ do {
|
|
|
+ // 分批查询:只查 is_kh 不为空的数据
|
|
|
+ $rows = DB::table('tmp_item_road')
|
|
|
+ ->where('id', '>', $lastId)
|
|
|
+ ->orderBy('id')
|
|
|
+ ->limit($limit)
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'order_time',
|
|
|
+ 'channel_details',
|
|
|
+ 'payment_amount',
|
|
|
+ 'price_4_total',
|
|
|
+ 'is_kh',
|
|
|
+ 'employee_id_2'
|
|
|
+ ])
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($rows->isEmpty()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 聚合到汇总数组
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $order_time = $row->order_time;
|
|
|
+ $month = strtotime(date("Y-m-01", $order_time));
|
|
|
+
|
|
|
+ $channel = $row->channel_details ?? '';
|
|
|
+
|
|
|
+ $t = $map[$row->employee_id_2] ?? [];
|
|
|
+ //收款销售收入
|
|
|
+ if (! $row->is_kh) {
|
|
|
+ if (!isset($paymentSummary[$month][$channel])) {
|
|
|
+ $paymentSummary[$month][$channel] = $row->payment_amount;
|
|
|
+ }else{
|
|
|
+ $paymentSummary[$month][$channel] = bcadd(
|
|
|
+ $paymentSummary[$month][$channel],
|
|
|
+ $row->payment_amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($paymentSummary_road[$month][$type])) {
|
|
|
+ $paymentSummary_road[$month][$type] = $row->payment_amount;
|
|
|
+ }else{
|
|
|
+ $paymentSummary_road[$month][$type] = bcadd(
|
|
|
+ $paymentSummary_road[$month][$type],
|
|
|
+ $row->payment_amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //成本
|
|
|
+ if (!isset($costSummary[$month][$channel])) {
|
|
|
+ $costSummary[$month][$channel] = $row->price_4_total;
|
|
|
+ }else{
|
|
|
+ $costSummary[$month][$channel] = bcadd(
|
|
|
+ $costSummary[$month][$channel],
|
|
|
+ $row->price_4_total,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary_road[$month][$type])) {
|
|
|
+ $costSummary_road[$month][$type] = $row->price_4_total;
|
|
|
+ }else{
|
|
|
+ $costSummary_road[$month][$type] = bcadd(
|
|
|
+ $costSummary_road[$month][$type],
|
|
|
+ $row->price_4_total,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新 lastId
|
|
|
+ $lastId = $rows->last()->id;
|
|
|
+
|
|
|
+ } while ($rows->count() === $limit);
|
|
|
+
|
|
|
+ $limit = 500;
|
|
|
+ $lastId = 0;
|
|
|
+ do {
|
|
|
+ $rows = DB::table('tmp_item_cost')
|
|
|
+ ->where('id', '>', $lastId)
|
|
|
+ ->orderBy('id')
|
|
|
+ ->limit($limit)
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'order_time',
|
|
|
+ 'channel_details',
|
|
|
+ 'ck_type',
|
|
|
+ 'fy_code',
|
|
|
+ 'taxamount',
|
|
|
+ 'employee_id_1'
|
|
|
+ ])
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($rows->isEmpty()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $order_time = $row->order_time;
|
|
|
+ $month = strtotime(date("Y-m-01", $order_time));
|
|
|
+ $channel = $row->channel_details ?? '';
|
|
|
+ $amount = $row->taxamount;
|
|
|
+
|
|
|
+ $t = $map[$row->employee_id_1] ?? [];
|
|
|
+
|
|
|
+ if($row->fy_code == 7004){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['settle_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['settle_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['settle_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['settle_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type])) {
|
|
|
+ $costSummary2_road[$month][$type]['settle_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['settle_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['settle_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $gl_amount = bcmul($amount, 0.05,3);
|
|
|
+ if (! isset($costSummary2[$month][$channel]['gl_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['gl_amount'] = $gl_amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['gl_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['gl_amount'],
|
|
|
+ $gl_amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['gl_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['gl_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['gl_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['gl_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $wl_amount = bcmul($amount, 2.3,3);
|
|
|
+ if (! isset($costSummary2[$month][$channel]['wl_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['wl_amount'] = $wl_amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['wl_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['wl_amount'],
|
|
|
+ $wl_amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['wl_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['wl_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['wl_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['wl_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7005){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['ht_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['ht_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['ht_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['ht_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['ht_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['ht_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['ht_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['ht_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7006){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['zk_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['zk_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['zk_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['zk_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['zk_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['zk_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['zk_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['zk_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7007){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['cx_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['cx_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['cx_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['cx_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['cx_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['cx_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['cx_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['cx_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7013){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['tg_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['tg_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['tg_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['tg_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['tg_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['tg_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['tg_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['tg_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7012){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['cl_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['cl_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['cl_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['cl_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['cl_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['cl_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['cl_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['cl_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //20049 客情出库 60 赠品出库 20048 员工福利
|
|
|
+ if($row->fy_code == 7008 || $row->ck_type == 20049){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['kq_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['kq_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['kq_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['kq_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['kq_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['kq_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['kq_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['kq_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7009 || $row->ck_type == 60){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['zp_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['zp_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['zp_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['zp_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['zp_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['zp_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['zp_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['zp_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7010){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['gg_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['gg_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['gg_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['gg_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['gg_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['gg_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['gg_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['gg_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7011){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['kd_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['kd_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['kd_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['kd_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['kd_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['kd_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['kd_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['kd_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7001){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['xsqt_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['xsqt_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['xsqt_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['xsqt_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['xsqt_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['xsqt_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['xsqt_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['xsqt_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7003){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['ry_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['ry_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['ry_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['ry_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['ry_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['ry_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['ry_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['ry_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($row->fy_code == 7019 || $row->ck_type == 20048){
|
|
|
+ if (! isset($costSummary2[$month][$channel]['sb_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['sb_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['sb_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['sb_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['sb_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['sb_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['sb_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['sb_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $sj_amount = bcmul($amount,0.01,3);
|
|
|
+ if (! isset($costSummary2[$month][$channel]['sj_amount'])) {
|
|
|
+ $costSummary2[$month][$channel]['sj_amount'] = $sj_amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2[$month][$channel]['sj_amount'] = bcadd(
|
|
|
+ $costSummary2[$month][$channel]['sj_amount'],
|
|
|
+ $sj_amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($t)){
|
|
|
+ $type = $t['type'];
|
|
|
+ $c_t = $t['channel_details'];
|
|
|
+ if(in_array($channel, $c_t)){
|
|
|
+ if (!isset($costSummary2_road[$month][$type]['sj_amount'])) {
|
|
|
+ $costSummary2_road[$month][$type]['sj_amount'] = $amount;
|
|
|
+ }else{
|
|
|
+ $costSummary2_road[$month][$type]['sj_amount'] = bcadd(
|
|
|
+ $costSummary2_road[$month][$type]['sj_amount'],
|
|
|
+ $amount,
|
|
|
+ 3
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新 lastId
|
|
|
+ $lastId = $rows->last()->id;
|
|
|
+
|
|
|
+ } while ($rows->count() === $limit);
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ return [false, "初步汇总异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $start_timeStamp = $data['start_timeStamp'];
|
|
|
+ $end_timeStamp = $data['end_timeStamp'];
|
|
|
+
|
|
|
+ $result = $this->sumRes($paymentSummary,$costSummary,$costSummary2);
|
|
|
+ $result2 = $this->sumRes2($paymentSummary_road, $costSummary_road, $costSummary2_road);
|
|
|
+
|
|
|
+ DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $result, $result2) {
|
|
|
+ // 1. 先删除旧数据
|
|
|
+ ItemReport::where('time', '>=', $start_timeStamp)
|
|
|
+ ->where('time', '<=', $end_timeStamp)
|
|
|
+ ->delete();
|
|
|
+ ItemReportRoad::where('time', '>=', $start_timeStamp)
|
|
|
+ ->where('time', '<=', $end_timeStamp)
|
|
|
+ ->delete();
|
|
|
+
|
|
|
+ // 2. 插入新数据
|
|
|
+ ItemReport::insert($result);
|
|
|
+ ItemReportRoad::insert($result2);
|
|
|
+
|
|
|
+ });
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ return [false, "结果异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function sumRes($paymentSummary, $costSummary, $costSummary2){
|
|
|
+ // 合并三个汇总数组为最终结果
|
|
|
+ $result = [];
|
|
|
+
|
|
|
+ // 获取所有唯一的时间+渠道组合
|
|
|
+ $keys = [];
|
|
|
+ foreach (array_keys($paymentSummary) as $month) {
|
|
|
+ foreach (array_keys($paymentSummary[$month]) as $channel) {
|
|
|
+ $keys["$month|$channel"] = ['month' => $month, 'channel' => $channel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (array_keys($costSummary) as $month) {
|
|
|
+ foreach (array_keys($costSummary[$month]) as $channel) {
|
|
|
+ $keys["$month|$channel"] = ['month' => $month, 'channel' => $channel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (array_keys($costSummary2) as $month) {
|
|
|
+ foreach (array_keys($costSummary2[$month]) as $channel) {
|
|
|
+ $keys["$month|$channel"] = ['month' => $month, 'channel' => $channel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历所有组合,构建最终数据
|
|
|
+ foreach ($keys as $key => $info) {
|
|
|
+ $month = $info['month'];
|
|
|
+ $channel = $info['channel'];
|
|
|
+
|
|
|
+ // 收款销售收入(注意:原逻辑中 is_kh == false 才计入)
|
|
|
+ $receipt_amount = isset($paymentSummary[$month][$channel])
|
|
|
+ ? $paymentSummary[$month][$channel]
|
|
|
+ : '0.000';
|
|
|
+
|
|
|
+ // 业务成本(来自 tmp_item_road 的 price_4_total)
|
|
|
+ $cost = isset($costSummary[$month][$channel])
|
|
|
+ ? $costSummary[$month][$channel]
|
|
|
+ : '0.000';
|
|
|
+
|
|
|
+ // 利润 = 收入 - 成本
|
|
|
+ $profit = bcsub($receipt_amount, $cost, 3);
|
|
|
+
|
|
|
+ // 从 costSummary2 提取各项费用(默认为 0.000)
|
|
|
+ $cs2 = $costSummary2[$month][$channel] ?? [];
|
|
|
+
|
|
|
+ $settle_amount = $cs2['settle_amount'] ?? '0.000';
|
|
|
+ $gl_amount = $cs2['gl_amount'] ?? '0.000';
|
|
|
+ $wl_amount = $cs2['wl_amount'] ?? '0.000';
|
|
|
+ $ht_amount = $cs2['ht_amount'] ?? '0.000';
|
|
|
+ $zk_amount = $cs2['zk_amount'] ?? '0.000';
|
|
|
+ $cx_amount = $cs2['cx_amount'] ?? '0.000';
|
|
|
+ $tg_amount = $cs2['tg_amount'] ?? '0.000';
|
|
|
+ $cl_amount = $cs2['cl_amount'] ?? '0.000';
|
|
|
+ $kq_amount = $cs2['kq_amount'] ?? '0.000';
|
|
|
+ $zp_amount = $cs2['zp_amount'] ?? '0.000';
|
|
|
+ $gg_amount = $cs2['gg_amount'] ?? '0.000';
|
|
|
+ $kd_amount = $cs2['kd_amount'] ?? '0.000';
|
|
|
+ $xsqt_amount = $cs2['xsqt_amount'] ?? '0.000';
|
|
|
+ $ry_amount = $cs2['ry_amount'] ?? '0.000';
|
|
|
+ $sb_amount = $cs2['sb_amount'] ?? '0.000';
|
|
|
+ $sj_amount = $cs2['sj_amount'] ?? '0.000';
|
|
|
+
|
|
|
+ $result[] = [
|
|
|
+ 'time' => $month,
|
|
|
+ 'channel_details'=> $channel,
|
|
|
+ 'receipt_amount' => $receipt_amount,
|
|
|
+ 'cost' => $cost,
|
|
|
+ 'profit' => $profit,
|
|
|
+ 'settle_amount' => $settle_amount,
|
|
|
+ 'gl_amount' => $gl_amount,
|
|
|
+ 'wl_amount' => $wl_amount,
|
|
|
+ 'ht_amount' => $ht_amount,
|
|
|
+ 'zk_amount' => $zk_amount,
|
|
|
+ 'cx_amount' => $cx_amount,
|
|
|
+ 'tg_amount' => $tg_amount,
|
|
|
+ 'cl_amount' => $cl_amount,
|
|
|
+ 'kq_amount' => $kq_amount,
|
|
|
+ 'zp_amount' => $zp_amount,
|
|
|
+ 'gg_amount' => $gg_amount,
|
|
|
+ 'kd_amount' => $kd_amount,
|
|
|
+ 'xsqt_amount' => $xsqt_amount,
|
|
|
+ 'ry_amount' => $ry_amount,
|
|
|
+ 'sb_amount' => $sb_amount,
|
|
|
+ 'sj_amount' => $sj_amount,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function sumRes2($paymentSummary, $costSummary, $costSummary2){
|
|
|
+ // 合并三个汇总数组为最终结果
|
|
|
+ $result = [];
|
|
|
+
|
|
|
+ // 获取所有唯一的时间+渠道组合
|
|
|
+ $keys = [];
|
|
|
+ foreach (array_keys($paymentSummary) as $month) {
|
|
|
+ foreach (array_keys($paymentSummary[$month]) as $channel) {
|
|
|
+ $keys["$month|$channel"] = ['month' => $month, 'channel' => $channel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (array_keys($costSummary) as $month) {
|
|
|
+ foreach (array_keys($costSummary[$month]) as $channel) {
|
|
|
+ $keys["$month|$channel"] = ['month' => $month, 'channel' => $channel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (array_keys($costSummary2) as $month) {
|
|
|
+ foreach (array_keys($costSummary2[$month]) as $channel) {
|
|
|
+ $keys["$month|$channel"] = ['month' => $month, 'channel' => $channel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历所有组合,构建最终数据
|
|
|
+ foreach ($keys as $key => $info) {
|
|
|
+ $month = $info['month'];
|
|
|
+ $channel = $info['channel'];
|
|
|
+
|
|
|
+ // 收款销售收入
|
|
|
+ $receipt_amount = isset($paymentSummary[$month][$channel])
|
|
|
+ ? $paymentSummary[$month][$channel]
|
|
|
+ : '0.000';
|
|
|
+
|
|
|
+ // 业务成本(来自 tmp_item_road 的 price_4_total)
|
|
|
+ $cost = isset($costSummary[$month][$channel])
|
|
|
+ ? $costSummary[$month][$channel]
|
|
|
+ : '0.000';
|
|
|
+
|
|
|
+ // 利润 = 收入 - 成本
|
|
|
+ $profit = bcsub($receipt_amount, $cost, 3);
|
|
|
+
|
|
|
+ // 从 costSummary2 提取各项费用(默认为 0.000)
|
|
|
+ $cs2 = $costSummary2[$month][$channel] ?? [];
|
|
|
+
|
|
|
+ $settle_amount = $cs2['settle_amount'] ?? '0.000';
|
|
|
+ $gl_amount = $cs2['gl_amount'] ?? '0.000';
|
|
|
+ $wl_amount = $cs2['wl_amount'] ?? '0.000';
|
|
|
+ $ht_amount = $cs2['ht_amount'] ?? '0.000';
|
|
|
+ $zk_amount = $cs2['zk_amount'] ?? '0.000';
|
|
|
+ $cx_amount = $cs2['cx_amount'] ?? '0.000';
|
|
|
+ $tg_amount = $cs2['tg_amount'] ?? '0.000';
|
|
|
+ $cl_amount = $cs2['cl_amount'] ?? '0.000';
|
|
|
+ $kq_amount = $cs2['kq_amount'] ?? '0.000';
|
|
|
+ $zp_amount = $cs2['zp_amount'] ?? '0.000';
|
|
|
+ $gg_amount = $cs2['gg_amount'] ?? '0.000';
|
|
|
+ $kd_amount = $cs2['kd_amount'] ?? '0.000';
|
|
|
+ $xsqt_amount = $cs2['xsqt_amount'] ?? '0.000';
|
|
|
+ $ry_amount = $cs2['ry_amount'] ?? '0.000';
|
|
|
+ $sb_amount = $cs2['sb_amount'] ?? '0.000';
|
|
|
+ $sj_amount = $cs2['sj_amount'] ?? '0.000';
|
|
|
+
|
|
|
+ $result[] = [
|
|
|
+ 'time' => $month,
|
|
|
+ 'receipt_amount' => $receipt_amount,
|
|
|
+ 'cost' => $cost,
|
|
|
+ 'profit' => $profit,
|
|
|
+ 'settle_amount' => $settle_amount,
|
|
|
+ 'gl_amount' => $gl_amount,
|
|
|
+ 'wl_amount' => $wl_amount,
|
|
|
+ 'ht_amount' => $ht_amount,
|
|
|
+ 'zk_amount' => $zk_amount,
|
|
|
+ 'cx_amount' => $cx_amount,
|
|
|
+ 'tg_amount' => $tg_amount,
|
|
|
+ 'cl_amount' => $cl_amount,
|
|
|
+ 'kq_amount' => $kq_amount,
|
|
|
+ 'zp_amount' => $zp_amount,
|
|
|
+ 'gg_amount' => $gg_amount,
|
|
|
+ 'kd_amount' => $kd_amount,
|
|
|
+ 'xsqt_amount' => $xsqt_amount,
|
|
|
+ 'ry_amount' => $ry_amount,
|
|
|
+ 'sb_amount' => $sb_amount,
|
|
|
+ 'sj_amount' => $sj_amount,
|
|
|
+ 'employee_title_type' => $channel
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
}
|