U8ServerService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\Depart;
  6. use App\Model\Employee;
  7. use App\Model\Product;
  8. use App\Model\PurchaseOrder;
  9. use App\Model\PurchaseOrderInfo;
  10. use App\Model\SalesOrder;
  11. use App\Model\SalesOrderProductInfo;
  12. use App\Model\SeeRange;
  13. use App\Model\Setting;
  14. use App\Model\Supplier;
  15. use App\Model\U8Job;
  16. use Illuminate\Support\Facades\Config;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Redis;
  20. class U8ServerService extends Service
  21. {
  22. public $db = null;
  23. public $error = null; // 错误信息
  24. public $u8 = []; //u8 配置连接参数
  25. public $u8_api = ""; //u8接口请求地址
  26. public $post_common = [];//u8接口公用参数
  27. public function __construct($is_need_connect = 0)
  28. {
  29. //u8 配置连接参数 u8_api设置
  30. list($status,$msg) = $this->settingConnection();
  31. if(! $status) {
  32. $this->error = $msg;
  33. return;
  34. }
  35. //是否需要连接u8数据库
  36. if($is_need_connect){
  37. //构建数据库连接对象
  38. list($status,$msg) = $this->settingDb();
  39. if(! $status) {
  40. $this->error = $msg;
  41. }
  42. }
  43. }
  44. //设置u8连接参数
  45. private function settingConnection(){
  46. $u8 = Setting::where('setting_name','u8')->where('setting_value','<>','')->first();
  47. if(empty($u8)) return [false, 'u8配置参数不存在!'];
  48. $u8 = $u8->toArray();
  49. // 使用 eval() 函数执行字符串并转换为数组
  50. $u8 = eval("return {$u8['setting_value']};");
  51. if(empty($u8['domain'])) return [false, '外部域名不能为空!'];
  52. if(empty($u8['u8_api_port'])) return [false, 'u8程序API端口不能为空!'];
  53. if(empty($u8['u8_database_port'])) return [false, 'u8程序数据库端口不能为空!'];
  54. if(empty($u8['database'])) return [false, 'u8程序数据库不能为空!'];
  55. if(empty($u8['database_account'])) return [false, 'u8程序数据库登录账号不能为空!'];
  56. if(empty($u8['database_password'])) return [false, 'u8程序数据库登录密码不能为空!'];
  57. if(empty($u8['sAccID'])) return [false, 'u8程序sAccID不能为空!'];
  58. if(empty($u8['sServer'])) return [false, 'u8程序sServer不能为空!'];
  59. if(empty($u8['sUserID'])) return [false, 'u8程序sUserID不能为空!'];
  60. if(empty($u8['sPassword'])) return [false, 'u8程序sPassword不能为空!'];
  61. $this->u8 = $u8;
  62. $this->u8_api = "https://" . $u8['domain'] . ":" . $u8['u8_api_port'] . "/U8Sys/U8API";
  63. $this->post_common = [
  64. "password"=>"cloud@123456",
  65. "entity"=>"", //调用方法
  66. "login"=>[
  67. "sAccID"=> $u8['sAccID'],
  68. "sDate"=> date("Y-m-d"),
  69. "sServer"=> $u8['sServer'],
  70. "sUserID"=> $u8['sUserID'],
  71. "sSerial"=> "",
  72. "sPassword"=> $u8['sPassword']
  73. ]
  74. ];
  75. return [true, ''];
  76. }
  77. //设置u8数据库连接
  78. private function settingDb(){
  79. if(empty($this->db)){
  80. $u8 = $this->u8;
  81. $config = [
  82. 'driver' => 'sqlsrv',
  83. 'host' => $u8['domain'],
  84. 'port' => $u8['u8_database_port'],
  85. 'database' => $u8['database'],
  86. 'username' => $u8['database_account'],
  87. 'password' => $u8['database_password'],
  88. ];
  89. // 数据库配置设置
  90. Config::set('database.connections.sqlsrvs', $config);
  91. // 连接
  92. try {
  93. $pdo = DB::connection('sqlsrvs')->getPdo();
  94. if ($pdo instanceof \PDO) {
  95. // 连接成功的逻辑代码
  96. $this->db = DB::connection('sqlsrvs');
  97. } else {
  98. return [false, '连接失败!'];
  99. }
  100. } catch (\Throwable $e) {
  101. return [false, $e->getMessage()];
  102. }
  103. }
  104. return [true, ''];
  105. }
  106. //采购订单保存
  107. public function U8PO_PomainSave($data,$cmaker = ""){
  108. if(! is_array($data)) $data = [$data];
  109. $id = $data;
  110. //映射ip是否通畅
  111. $bool = $this->isDomainAvailable($this->u8['domain']);
  112. if(! $bool) {
  113. $msg = 'U8程序外部域名不可达';
  114. $this->finalSettle($id, U8Job::one,$msg);
  115. return;
  116. }
  117. //获取数据
  118. $result = $this->getPurchaseData($id);
  119. if(empty($result)) {
  120. $msg = "同步数据获取失败";
  121. $this->finalSettle($id, U8Job::one, $msg);
  122. return;
  123. }
  124. //u8接口参数组织
  125. $post = $this->post_common;
  126. $post['entity'] = "U8PO_PomainSave";
  127. $time = date("Y-m-d");
  128. foreach ($result as $value){
  129. $bodys = [];
  130. foreach ($value['product'] as $son){
  131. //子表数据
  132. $bodys[] = [
  133. "iappids"=>"", //子表id
  134. "cinvcode"=>$son['code'], //存货编码
  135. "iquantity"=>$son['number'], //数量
  136. "inum"=>$son['number'], //件数
  137. "ipertaxrate"=>$son['ipertaxrate'], //税率
  138. "iunitprice"=>$son['iunitprice'], //原币单价
  139. "itaxprice"=>$son['itaxprice'], //原币含税单价
  140. "isum"=>$son['isum'], //原币价税合计
  141. "imoney"=>$son['imoney'], //原币无税金额
  142. "itax"=>$son['itax'],//原币税额
  143. "cbmemo"=>$son['mark'], //表体备注
  144. "cdefine22"=>"",
  145. "cdefine23"=>"",
  146. "cdefine24"=>"",
  147. "cdefine25"=>"",
  148. "cdefine26"=>"",
  149. "cdefine27"=>"",
  150. "cdefine28"=>"",
  151. "cdefine29"=>"",
  152. "cdefine30"=>"",
  153. "cdefine31"=>"",
  154. "cdefine32"=>"",
  155. "cdefine33"=>"",
  156. "cdefine34"=>"",
  157. "cdefine35"=>"",
  158. "cdefine36"=>"",
  159. "cdefine37"=>"",
  160. "cfree1"=>"",
  161. "cfree2"=>"",
  162. "cfree3"=>"",
  163. "cfree4"=>"",
  164. "cfree5"=>"",
  165. "cfree6"=>"",
  166. "cfree7"=>"",
  167. "cfree8"=>"",
  168. "cfree9"=>"",
  169. "cfree10"=>""
  170. ];
  171. }
  172. //最终数据
  173. $post['data'] = [
  174. "cpoid"=>"",
  175. "dpodate"=>date("Y-m-d",$value['crt_time']),
  176. "cmemo"=>$value['mark'], //"T9采购单:" . $value['order_number']
  177. "cmaker"=>$cmaker ?? 'admin',
  178. "cmaketime"=>$time,
  179. "IsExamine"=>false,
  180. "cptname"=>$value['cptname']??"",//采购类型
  181. "cvencode"=>"", //供应商编码
  182. "cvenname"=>$value['cvenname'], //供应商名称
  183. "cdepcode"=>"", //部门编号
  184. "cdepname"=>"", //部门名称 $value['cdepname']
  185. "cpersoncode"=>"", //业务员编码jobnumber
  186. "jobnumber"=>$value['jobnumber'], //业务员编码
  187. "cdefine1"=>"",
  188. "cdefine2"=>"",
  189. "cdefine3"=>"",
  190. "cdefine4"=>"",
  191. "cdefine5"=>"",
  192. "cdefine6"=>"",
  193. "cdefine7"=>"",
  194. "cdefine8"=>"",
  195. "cdefine9"=>"",
  196. "cdefine10"=>"",
  197. "cdefine11"=>"",
  198. "cdefine12"=>"",
  199. "cdefine13"=>"",
  200. "cdefine14"=>"",
  201. "cdefine15"=>"",
  202. "cdefine16"=>"",
  203. "bodys"=>$bodys
  204. ];
  205. file_put_contents('record_purchase.txt',"请求参数:" . json_encode($post) . PHP_EOL,8);
  206. $return = $this->post_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  207. file_put_contents('record_purchase.txt',"返回结果:" . json_encode($return). PHP_EOL,8);
  208. //剔除数据
  209. $id = array_diff($id, [$value['id']]);
  210. if(empty($return)) {
  211. $msg = '异常错误,请确认请求接口地址或地址不可达';
  212. $this->finalSettle($value['id'], U8Job::one, $msg);
  213. }else{
  214. if( ! empty($return['flag'])){
  215. $this->finalSettle($value['id'], U8Job::one);
  216. }else{
  217. $this->finalSettle($value['id'], U8Job::one, $return['msg']);
  218. }
  219. }
  220. }
  221. if(! empty($id)){
  222. $msg = "未找到同步数据";
  223. $this->finalSettle($id, U8Job::one, $msg);
  224. }
  225. }
  226. //销售订单(合同)保存
  227. public function U8SaleOrderSave($data,$cmaker = ""){
  228. if(! is_array($data)) $data = [$data];
  229. $id = $data;
  230. //映射ip是否通畅
  231. $bool = $this->isDomainAvailable($this->u8['domain']);
  232. if(! $bool) {
  233. $msg = 'U8程序外部域名不可达';
  234. $this->finalSettle($id, U8Job::two, $msg);
  235. return;
  236. }
  237. //获取数据
  238. $result = $this->getSaleOrderData($id);
  239. if(empty($result)) {
  240. $msg = "同步数据获取失败";
  241. $this->finalSettle($id, U8Job::two, $msg);
  242. return;
  243. }
  244. //u8接口参数组织
  245. $post = $this->post_common;
  246. $post['entity'] = "U8SaleOrderSave";
  247. $time = date("Y-m-d");
  248. $time1 = date("Y-m-d H:i:s");
  249. foreach ($result as $value){
  250. $bodys = [];
  251. $cdefine31 = "";
  252. if($value['model_type'] == SalesOrder::Model_type_two) $cdefine31 = $value['ccusabbname'];
  253. foreach ($value['product'] as $son){
  254. //子表数据
  255. $bodys[] = [
  256. "cinvcode"=>$son['code'], //存货编码
  257. "iquantity"=>$son['number'], //数量
  258. "inum"=>$son['number'], //件数
  259. "itaxrate"=>$son['itaxrate'], //税率
  260. "iunitprice"=>$son['iunitprice'],//原币单价
  261. "itaxunitprice"=>$son['itaxunitprice'], // 原币含税单价
  262. "isum"=>$son['isum'], //原币价税合计
  263. "imoney"=>$son['imoney'], //原币无税金额
  264. "itax"=>$son['itax'],//原币税额
  265. "cbmemo"=>$son['mark'], //表体备注
  266. // "iappids"=>"", //子表id
  267. // "cinvcode"=>$son['code'], //存货编码
  268. // "iquantity"=>$son['number'], //数量
  269. // "inum"=>$son['number'], //件数
  270. // "ipertaxrate"=>$son['ipertaxrate'], //税率
  271. // "iunitprice"=>$son['iunitprice'], //原币单价
  272. // "itaxprice"=>$son['itaxprice'], //原币含税单价
  273. // "isum"=>$son['isum'], //原币价税合计
  274. // "imoney"=>$son['imoney'], //原币无税金额
  275. // "itax"=>$son['itax'],//原币税额
  276. // "cbmemo"=>$son['mark'], //表体备注
  277. "cdefine22"=>$son['cdefine22'], //手机号码
  278. "cdefine25"=>$son['cdefine25'], //平台类型
  279. "cdefine28"=>$son['cdefine28'], //平台单号
  280. "cdefine29"=>$son['cdefine29'], //分社施工
  281. "cdefine30"=>$son['cdefine30'], //业务员
  282. "cdefine31"=>$cdefine31 ? $cdefine31 : $son['cdefine31'],//客户名称
  283. "cdefine32"=>$son['cdefine32'], //达人昵称
  284. "cdefine33"=>"",
  285. "cdefine34"=>"",
  286. "cdefine35"=>"",
  287. "cdefine36"=>"",
  288. "cdefine37"=>"",
  289. "cfree1"=>"",
  290. "cfree2"=>"",
  291. "cfree3"=>"",
  292. "cfree4"=>"",
  293. "cfree5"=>"",
  294. "cfree6"=>"",
  295. "cfree7"=>"",
  296. "cfree8"=>"",
  297. "cfree9"=>"",
  298. "cfree10"=>""
  299. ];
  300. }
  301. //最终数据
  302. $post['data'] = [
  303. "csocode"=>'',
  304. "ddate"=> $time,
  305. "cmaker"=>$cmaker ?? 'admin',
  306. "dcreatesystime"=> $time1,
  307. "cstcode"=>"",
  308. "cbustype" => $value['cbustype'], //业务类型
  309. "cstname"=>$value['cstname'], //销售类型
  310. "ccuscode"=>"",
  311. "ccusabbname"=>$value['ccusabbname'], //客户简称
  312. "cdepcode"=>"",
  313. "cdepname"=>"", // 部门名称 $value['cdepname']
  314. "cpersoncode"=>"", //业务员编码 暂时不要
  315. "jobnumber"=>$value['jobnumber'],//业务员工号
  316. "itaxrate"=>"0",
  317. "cmemo"=>$value['mark'],//"T9销售订单:". $value['order_number']
  318. "cdefine1"=>"",
  319. "cdefine2"=>"",
  320. "cdefine3"=>"",
  321. "cdefine4"=>"",
  322. "cdefine5"=>"",
  323. "cdefine6"=>"",
  324. "cdefine7"=>"",
  325. "cdefine8"=>"",
  326. "cdefine9"=>"",
  327. "cdefine10"=>"",
  328. "cdefine11"=>"",
  329. "cdefine12"=>"",
  330. "cdefine13"=>"",
  331. "cdefine14"=>"",
  332. "cdefine15"=>"",
  333. "cdefine16"=>"",
  334. "bodys"=>$bodys
  335. ];
  336. file_put_contents('record_purchase.txt',"请求参数:" . json_encode($post) . PHP_EOL,8);
  337. $return = $this->post_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  338. file_put_contents('record_purchase.txt',"返回结果:" . json_encode($return). PHP_EOL,8);
  339. //剔除数据
  340. $id = array_diff($id, [$value['id']]);
  341. if(empty($return)) {
  342. $msg = '异常错误,请确认请求接口地址或地址不可达';
  343. $this->finalSettle($value['id'],U8Job::two, $msg);
  344. }else{
  345. if( ! empty($return['flag'])){
  346. $this->finalSettle($value['id'],U8Job::two);
  347. }else{
  348. $this->finalSettle($value['id'], U8Job::two, $return['msg']);
  349. }
  350. }
  351. }
  352. if(! empty($id)){
  353. $msg = "未找到同步数据";
  354. $this->finalSettle($id,U8Job::two, $msg);
  355. }
  356. }
  357. //最终处理
  358. public function finalSettle($data,$data_type, $msg = ''){
  359. if(! is_array($data)) $data = [$data];
  360. $time = time();
  361. $insert = [];
  362. U8Job::where('del_time',0)
  363. ->where('data_type',$data_type)
  364. ->whereIn('data',$data)
  365. ->update(['del_time' => $time]);
  366. foreach ($data as $value){
  367. if(empty($msg)){
  368. $insert[] = [
  369. 'data' => $value,
  370. 'data_type' => $data_type,
  371. 'crt_time' => $time,
  372. 'state' => U8Job::success,
  373. ];
  374. }else{
  375. $insert[] = [
  376. 'data' => $value,
  377. 'data_type' => $data_type,
  378. 'crt_time' => $time,
  379. 'state' => U8Job::failed,
  380. 'msg' => $msg
  381. ];
  382. }
  383. }
  384. U8Job::insert($insert);
  385. }
  386. public function getPurchaseData($id){
  387. $main = PurchaseOrder::whereIn('id',$id)
  388. ->where('del_time',0)
  389. ->get()->toArray();
  390. if(empty($main)) return [];
  391. $supplier = Supplier::whereIn('id',array_unique(array_column($main,'supplier')))
  392. ->pluck('title','id')
  393. ->toArray();
  394. // $depart = Depart::whereIn('id',array_unique(array_column($main,'depart_id')))
  395. // ->pluck('title','id')
  396. // ->toArray();
  397. $emp = Employee::whereIn('id',array_unique(array_column($main,'purchase_id')))
  398. ->pluck('number','id')
  399. ->toArray();
  400. $code_map = BasicType::whereIn('id',array_unique(array_column($main,'purchase_type')))
  401. ->pluck('title','id')
  402. ->toArray();
  403. $sub = PurchaseOrderInfo::whereIn('purchase_order_id',$id)
  404. ->where('del_time',0)
  405. ->get()->toArray();
  406. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  407. ->get()->toArray();
  408. $product_map = array_column($product,null,'id');
  409. $sub_map = [];
  410. foreach ($sub as $value){
  411. $product_tmp = $product_map[$value['product_id']] ?? [];
  412. $value['code'] = $product_tmp['code'];
  413. //计算金额
  414. if($value['rate'] > 0){
  415. // ipertaxrate 税率
  416. // iunitprice 原币单价
  417. // itaxprice 原币含税单价
  418. // isum 原币价税合计
  419. // imoney 原币无税金额
  420. // itax 原币税额
  421. $value['ipertaxrate'] = $value['rate'];
  422. $rate = round($value['rate'] / 100,2);
  423. $value['iunitprice'] = round($value['price'] / (1 + $rate),2);
  424. $value['itaxprice'] = $value['price'];
  425. $value['isum'] = round($value['price'] * $value['number'],2);
  426. $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  427. $value['itax'] = round($value['isum'] - $value['imoney'],2);
  428. }else{
  429. $value['ipertaxrate'] = 0;
  430. $value['iunitprice'] = $value['price'];
  431. $value['itaxprice'] = $value['price'];
  432. $value['isum'] = $value['price'] * $value['number'];
  433. $value['imoney'] = $value['isum'];
  434. $value['itax'] = 0;
  435. }
  436. $sub_map[$value['purchase_order_id']][] = $value;
  437. }
  438. foreach ($main as $key => $value){
  439. $main[$key]['cptname'] = $code_map[$value['purchase_type']] ?? "";
  440. $main[$key]['cvenname'] = $supplier[$value['supplier']] ?? "";
  441. // $main[$key]['cdepname'] = $depart[$value['depart_id']] ?? "";
  442. // $main[$key]['cpersoncode'] = $emp[$value['purchase_id']] ?? "";
  443. $main[$key]['jobnumber'] = $emp[$value['purchase_id']] ?? "";
  444. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  445. }
  446. return $main;
  447. }
  448. public function getSaleOrderData($id){
  449. $main = SalesOrder::whereIn('id',$id)
  450. ->where('del_time',0)
  451. ->get()->toArray();
  452. if(empty($main)) return [];
  453. $main_map = array_column($main,null,'id');
  454. $sub = SalesOrderProductInfo::whereIn('sales_order_id',$id)
  455. ->where('del_time',0)
  456. ->get()->toArray();
  457. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  458. ->get()->toArray();
  459. $product_map = array_column($product,null,'id');
  460. $code_id = array_filter(array_unique(array_merge_recursive(array_column($main,'sale_type'),array_column($main,'plat_type'),array_column($main,'install_position'),array_column($main,'customer_short_name'))));
  461. $code_map = BasicType::whereIn('id',$code_id)
  462. ->pluck('title','id')
  463. ->toArray();
  464. $customer_map = Customer::whereIn('id',array_unique(array_column($main,'customer_id')))
  465. ->pluck('title','id')
  466. ->toArray();
  467. $empList = Employee::whereIn('id',array_unique(array_column($main,'crt_id')))
  468. ->select('number','id','emp_name')
  469. ->get()
  470. ->toArray();
  471. $emp = array_column($empList,'number','id');
  472. $emp2 = array_column($empList,'emp_name','id');
  473. $sub_map = [];
  474. foreach ($sub as $value){
  475. $product_tmp = $product_map[$value['product_id']] ?? [];
  476. $main_tmp = $main_map[$value['sales_order_id']] ?? [];
  477. $position = $code_map[$main_tmp['install_position']] ?? "";
  478. $cdefine25 = $code_map[$main_tmp['plat_type']] ?? "";//平台类型
  479. $cdefine31 = $customer_map[$main_tmp['customer_id']] ?? "";
  480. $cdefine28 = $cdefine29 = $cdefine30 = $cdefine32 = "";
  481. if($main_tmp['model_type'] == SalesOrder::Model_type_four){
  482. //线上订单
  483. $cdefine28 = $main_tmp['plat_order'] ?? "";//平台单号
  484. $cdefine29 = $main_tmp['cdefine29'] ?? "";
  485. $cdefine30 = $main_tmp['cdefine30'] ?? "";
  486. $cdefine32 = $main_tmp['cdefine32'] ?? "";
  487. }elseif($main_tmp['model_type'] == SalesOrder::Model_type_two){
  488. //分社订货
  489. $purchase_order = PurchaseOrder::where('del_time',0)
  490. ->where('order_number',$main_tmp['contact_order_no'])
  491. ->first();
  492. if(! empty($purchase_order)) $cdefine28 = SalesOrder::where('id',$purchase_order->sales_order_id)->value('order_number') ?? "";
  493. $cdefine25 = "渠道部-分社";
  494. $depart_tmp = Depart::where('id', $main_tmp['top_depart_id'])->first();
  495. if(! empty($depart_tmp)) {
  496. $depart_tmp = $depart_tmp->toArray();
  497. if($depart_tmp['channel_id'] > 0) $cdefine30 = Employee::where('id',$depart_tmp['channel_id'])->value('emp_name');
  498. }
  499. }else{
  500. $cdefine28 = $main_tmp['order_number'] ?? "";
  501. $cdefine29 = $position;
  502. $cdefine30 = $emp2[$main_tmp['crt_id']] ?? "";
  503. }
  504. // "itaxrate"=>$son['itaxrate'], //税率
  505. // "iunitprice"=>$son['iunitprice'],//原币单价
  506. // "itaxunitprice"=>$son['itaxunitprice'], // 原币含税单价
  507. // "isum"=>$son['isum'], //原币价税合计
  508. // "imoney"=>$son['imoney'], //原币无税金额
  509. // "itax"=>$son['itax'],//原币税额
  510. //计算金额
  511. //比如这4个产品合同金额是300.11,那么价税合计就是300.11,
  512. //含税单价就是300.11/4,目前加了税率的没有计算规则。税率不进入计算
  513. $value['itaxrate'] = 0;//税率
  514. $value['iunitprice'] = $value['price'];
  515. $value['itaxunitprice'] = $value['price'];
  516. $value['isum'] = $value['final_amount']; //原币价税合计
  517. $value['imoney'] = $value['isum']; //原币无税金额
  518. $value['itax'] = 0;
  519. // if($value['rate'] > 0){
  520. // $value['itaxrate'] = $value['rate'];
  521. // $rate = round($value['rate'] / 100,2);
  522. // $value['iunitprice'] = round($value['final_amount'] / (1 + $rate),2);
  523. // $value['itaxunitprice'] = $value['final_amount'];
  524. // $value['isum'] = round($value['final_amount'] * $value['number'],2);
  525. // $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  526. // $value['itax'] = round($value['isum'] - $value['imoney'],2);
  527. // }else{
  528. // $value['itaxrate'] = 0;
  529. // $value['iunitprice'] = $value['final_amount'];
  530. // $value['itaxunitprice'] = $value['final_amount'];
  531. // $value['isum'] = $value['final_amount'] * $value['number'];
  532. // $value['imoney'] = $value['isum'];
  533. // $value['itax'] = 0;
  534. // }
  535. $value['cdefine25'] = $cdefine25;//平台类型
  536. $value['cdefine28'] = $cdefine28; //平台单号
  537. $value['cdefine29'] = $cdefine29;//分社施工
  538. $value['cdefine32'] = $cdefine32;//达人昵称
  539. $value['cdefine31'] = $cdefine31;//客户名称
  540. $value['cdefine22'] = $main_tmp['customer_contact'] ?? "";//手机号码
  541. $value['cdefine30'] = $cdefine30;//业务员
  542. $value['code'] = $product_tmp['code'];//存货编码
  543. $sub_map[$value['sales_order_id']][] = $value;
  544. }
  545. foreach ($main as $key => $value){
  546. $customer_short_name = $code_map[$value['customer_short_name']] ?? "";
  547. if($value['model_type'] == SalesOrder::Model_type_two && $value['customer_short_name'] == 0) {
  548. $purchase_tmp = PurchaseOrder::where('order_number',$value['contact_order_no'])->value("top_depart_id");
  549. $customer_short_name = Depart::where('id',$purchase_tmp)->value('title');
  550. }
  551. $main[$key]['cbustype'] = "普通销售"; //业务类型(本身就是中文)
  552. $main[$key]['cstname'] = SalesOrder::$model_type_title_u8[$value['model_type']] ?? ""; //销售类型
  553. $main[$key]['ccusabbname'] = $customer_short_name;//客户简称
  554. // $main[$key]['cdepname'] = $depart[$value['top_depart_id']] ?? "";//部门名称
  555. // $main[$key]['cpersoncode'] = $emp[$value['crt_id']] ?? "";//业务员
  556. // $main[$key]['jobnumber'] = $emp[$value['crt_id']] ?? "";
  557. if($value['model_type'] == SalesOrder::Model_type_one){
  558. $main[$key]['jobnumber'] = "T90043";
  559. }elseif ($value['model_type'] == SalesOrder::Model_type_two){
  560. $main[$key]['jobnumber'] = "T90022";
  561. }elseif ($value['model_type'] == SalesOrder::Model_type_four){
  562. $main[$key]['jobnumber'] = "T90044";
  563. }else{
  564. $main[$key]['jobnumber'] = "T90000";
  565. }
  566. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  567. }
  568. return $main;
  569. }
  570. public function getMessage($id,$type){
  571. $result = U8Job::where('del_time',0)
  572. ->whereIn('data',$id)
  573. ->where('data_type',$type)
  574. ->select('data','state','crt_time','msg')
  575. ->get()->toArray();
  576. $return = [];
  577. foreach ($result as $value){
  578. if($value['state'] == 1){
  579. $return[$value['data']] = '同步成功';
  580. }else{
  581. $return[$value['data']] = '同步失败:' . $value['msg'];
  582. }
  583. }
  584. return $return;
  585. }
  586. public function post_helper($url, $data, $header = [], $timeout = 20){
  587. $ch = curl_init();
  588. curl_setopt($ch, CURLOPT_URL, $url);
  589. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  590. curl_setopt($ch, CURLOPT_ENCODING, '');
  591. curl_setopt($ch, CURLOPT_POST, 1);
  592. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  593. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  594. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  595. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  596. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  597. $r = curl_exec($ch);
  598. curl_close($ch);
  599. return json_decode($r, true);
  600. }
  601. }