| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Services;
- use App\OracleBasCustomer;
- use App\Owner;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- Class OwnerService
- {
- /*
- * array | string $column
- * 默认一些select字段,可传递string 或 array来指定select字段
- */
- public function getSelection($column = ['id', 'name'])
- {
- return Owner::filterAuthorities()->select($column)->get();
- }
- /**
- *同步WMS全部货主至WAS
- */
- public function syncOwnersData()
- {
- $basCustomers = OracleBasCustomer::query()
- ->select('CUSTOMERID', 'DESCR_C')
- ->where('DESCR_C', 'not like', '%换ERP%')
- ->where('DESCR_C', 'not like', '%退仓%')
- ->where('CUSTOMER_TYPE', 'OW')
- ->get();
- $ownerCount = Owner::count();
- if (count($basCustomers) == $ownerCount) return null;
- foreach ($basCustomers as $basCustomer) {
- $owner = Owner::where('code', $basCustomer['customerid'])->first();
- if (!isset($owner))
- Owner::query()->create([
- 'code' => $basCustomer['customerid'],
- 'name' => $basCustomer['descr_c'],
- 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
- ]);
- }
- $owners = Owner::query()->select('id', 'name')->get();
- return $owners;
- }
- public function first(array $params){
- $owner = Owner::query();
- foreach ($params as $column => $value){
- $owner->where($column, $value);
- }
- return $owner->first();
- }
- public function create(array $params){
- $owner = Owner::query()->create($params);
- return $owner;
- }
- public function firstOrCreate(array $params, array $values = null){
- if (!$values) return Owner::query()->firstOrCreate($params);
- return Owner::query()->firstOrCreate($params,$values);
- }
- // XXX 代码逻辑有待纠正
- public function 获取WMS订单列中对应的WAS货主列($WMSOrderHeaders)
- {
- $customerIds = array_unique(data_get($WMSOrderHeaders, '*.customerid'));
- $owners = Owner::query()->whereIn('code', $customerIds)->get();
- $codes = data_get($owners, '*.code');
- $codeDiff = array_diff($customerIds, $codes);
- $logs = [];
- $errs = [];
- if (count($codes) > count($customerIds)) {
- $basCustomer = OracleBasCustomer::query()->whereIn('customerid', $codeDiff)->get();
- foreach ($basCustomer as $customer) {
- if($customer['descr_c'] ?? false && $customer['customerid'] && false){
- try {
- $owner = Owner::query()->create(['name' => $customer['descr_c'], 'code' => $customer['customerid']]);
- $owners->push($owner);
- array_push($logs, ['info' => '创建「' . json_encode($owner) . '」']);
- } catch (\Exception $e) {
- array_push($errs, ['info' => '创建「' . json_encode($errs) . '」']);
- }
- }
- }
- }
- if(count($logs)>0)
- LogService::log(__METHOD__, __FUNCTION__, '批量创建客户「:' . json_encode($logs) . '」', Auth::user()['id']);
- if(count($errs)>0)
- LogService::log(__METHOD__, __FUNCTION__, '批量创建客户失败「:' . json_encode($errs) . '」', Auth::user()['id']);
- return $owners;
- }
- public function 获取订单跟踪的货主(){
- return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
- $query->where('status','启用');
- })->get();
- }
- public function getAuthorizedOwners(){
- $user = Auth::user();
- return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
- }
- }
|