U8ServerService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 = false)
  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_u8_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',date("Y-m-d H:i:s") . "请求参数:" . json_encode($post) . PHP_EOL,8);
  337. $return = $this->post_u8_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  338. file_put_contents('record_purchase.txt',date("Y-m-d H:i:s") ."返回结果:" . 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 getStock($code = [], $warehouse = ""){
  387. if(empty($code) || empty($warehouse)) return [false, '存货以及仓库不能为空'];
  388. //映射ip是否通畅
  389. $bool = $this->isDomainAvailable($this->u8['domain']);
  390. if(! $bool) return [false, 'U8程序外部域名不可达'];
  391. $result = $this->db->table('CurrentStock')
  392. ->where("cWhCode", $warehouse)
  393. ->whereIn("cInvCode", $code)
  394. ->select('iQuantity as number', 'cInvCode as product_no')
  395. ->get()->toArray();
  396. $return = [];
  397. foreach ($result as $value){
  398. $return[] = [
  399. 'number' => floatval($value->number),
  400. 'product_no' => $value->product_no
  401. ];
  402. }
  403. return [true, $return];
  404. }
  405. public function getSnList($data, $user){
  406. //映射ip是否通畅
  407. $bool = $this->isDomainAvailable($this->u8['domain']);
  408. if(! $bool) return [false, 'U8程序外部域名不可达'];
  409. if($data['sn_type'] == 1){
  410. list($status, $msg) = $this->getSnListFormU8One($this->db,$data);
  411. }else{
  412. //发货出库单 sn码
  413. list($status, $msg) = $this->getSnListFormU8Two($this->db,$data);
  414. }
  415. return [$status, $msg];
  416. }
  417. public function getSnListFormU8One($db, $data){
  418. if(empty($data['code'])) return [false, '存货不能为空'];
  419. $sn = $data['sn'] ?? "";
  420. $construction_id = $data['construction_id'] ?? 0;
  421. $warehouse = [
  422. '001',
  423. '003',
  424. '010',
  425. ];
  426. // $db->enableQueryLog();
  427. //检索条件 在库的
  428. $model = $db->table('ST_SNState')
  429. ->select('cinvCode as code','cInvSN as sn','AutoID as auto_id')
  430. ->whereIn("cWhCode", $warehouse)
  431. ->where("cInvCode", $data['code'])
  432. ->where("iSNState", 2)
  433. ->when(! empty($sn), function ($query) use ($sn) {
  434. return $query->where('cInvSN', 'LIKE', '%'.$sn.'%');
  435. })
  436. // ->when(! empty($construction_id), function ($query) use ($construction_id) {
  437. // return $query->whereNull('cSNDefine1')->orWhere('cSNDefine1', '')->orWhere('cSNDefine1', $construction_id);
  438. // })
  439. ->when(empty($construction_id), function ($query) {
  440. return $query->where(function ($q) {
  441. $q->whereNull('cSNDefine1')
  442. ->orWhere('cSNDefine1', '');
  443. });
  444. })
  445. ->orderBy('cSNDefine1','desc')
  446. ->orderBy('AutoID','asc');
  447. $list = $this->limit($model, '', $data);
  448. // dd($db->getQueryLog());
  449. return [true, $list];
  450. }
  451. public function getSnListFormU8Two($db, $data){
  452. if(empty($data['code']) || empty($data['depart_title'])) return [false, '存货以及门店信息不能为空'];
  453. $sn = $data['sn'] ?? "";
  454. $construction_id = $data['construction_id'] ?? 0;
  455. // $db->enableQueryLog();
  456. $model = $db->table('rdrecord32 as a')
  457. ->leftJoin('rdrecords32 as b','b.ID','a.ID')
  458. ->leftJoin('ST_SNDetail_SaleOut as c','c.iVouchsID','b.AutoID')
  459. ->select("c.cInvCode as code",'c.cinvSN as sn','c.AutoID as auto_id') //,'c.cSNDefine1','a.ID'
  460. ->whereNotNull('a.cHandler')
  461. ->where("b.cInvCode", $data['code'])
  462. ->where("b.iQuantity", '>', 0)
  463. ->where("b.cDefine31", $data['depart_title'])
  464. ->whereNotNull("c.cinvSN")
  465. ->when(! empty($sn), function ($query) use ($sn) {
  466. return $query->where('c.cInvSN', 'LIKE', '%'.$sn.'%');
  467. })
  468. // ->when(! empty($construction_id), function ($query) use ($construction_id) {
  469. // return $query->whereNull('c.cSNDefine1')->orWhere('c.cSNDefine1', '')->orWhere('c.cSNDefine1', $construction_id);
  470. // })
  471. ->when(empty($construction_id), function ($query) {
  472. return $query->where(function ($q) {
  473. $q->whereNull('c.cSNDefine1')
  474. ->orWhere('c.cSNDefine1', '');
  475. });
  476. })
  477. ->orderBy('c.cSNDefine1','desc')
  478. ->orderBy('a.ID','desc')
  479. ->orderBy('c.AutoID','asc');
  480. $list = $this->limit($model, '', $data);
  481. // $logs = $db->getQueryLog();dd($logs);
  482. return [true, $list];
  483. }
  484. public function getSnListFormU8ForMap($data){
  485. //映射ip是否通畅
  486. $bool = $this->isDomainAvailable($this->u8['domain']);
  487. if(! $bool) return [false, 'U8程序外部域名不可达'];
  488. $warehouse = [
  489. '001',
  490. '003',
  491. '010',
  492. ];
  493. $list = $this->db->table('ST_SNState')
  494. ->select("cInvCode as code",'cinvSN as sn')
  495. ->whereIn("cWhCode", $warehouse)
  496. ->whereIn("cInvCode", $data['code'])
  497. ->whereIn('cInvSN', $data['sn'])
  498. // ->where("iSNState", 2)
  499. ->get()->toArray();
  500. $return = [];
  501. foreach ($list as $value){
  502. $return[] = [
  503. 'code' => $value->code,
  504. 'sn' => $value->sn
  505. ];
  506. }
  507. return [true, $return];
  508. }
  509. public function saveSnUseDataDetail($sn_type, $sn_for_u8, $data_id){
  510. $bool = $this->isDomainAvailable($this->u8['domain']);
  511. if(! $bool) return [false, 'U8程序外部域名不可达'];
  512. $db = $this->db;
  513. try {
  514. DB::beginTransaction();
  515. if(empty($sn_for_u8)){//删除
  516. if($sn_type == 1){
  517. $db->table('ST_SNState')
  518. ->where('cSNDefine1', $data_id)
  519. ->update(['cSNDefine1' => ""]);
  520. }else{
  521. $db->table('ST_SNDetail_SaleOut')
  522. ->where('cSNDefine1', $data_id)
  523. ->update(['cSNDefine1' => ""]);
  524. }
  525. }else{//增加或更新
  526. if($sn_type == 1){
  527. $db->table('ST_SNState')
  528. ->whereIn('AutoID', $sn_for_u8)
  529. ->update(['cSNDefine1' => $data_id]);
  530. }else{
  531. $db->table('ST_SNDetail_SaleOut')
  532. ->whereIn('AutoID', $sn_for_u8)
  533. ->update(['cSNDefine1' => $data_id]);
  534. }
  535. }
  536. DB::commit();
  537. }catch (\Exception $exception){
  538. DB::rollBack();
  539. return [false, $exception->getMessage()];
  540. }
  541. return [true, ''];
  542. }
  543. public function getSnForWarrantyData($data){
  544. //映射ip是否通畅
  545. $bool = $this->isDomainAvailable($this->u8['domain']);
  546. if(! $bool) return [false, 'U8程序外部域名不可达'];
  547. $warehouse = [
  548. '001',
  549. '003',
  550. '010',
  551. ];
  552. $list = $this->db->table('ST_SNState')
  553. ->select("cInvCode as code",'cinvSN as sn','AutoID as auto_id')
  554. ->whereIn("cWhCode", $warehouse)
  555. ->where('cInvSN', $data['sn'])
  556. // ->where("iSNState", 2)
  557. ->first();
  558. $listArray = [];
  559. if (! empty($list)) $listArray = get_object_vars($list);
  560. return [true, $listArray];
  561. }
  562. public function getPurchaseData($id){
  563. $main = PurchaseOrder::whereIn('id',$id)
  564. ->where('del_time',0)
  565. ->get()->toArray();
  566. if(empty($main)) return [];
  567. $supplier = Supplier::whereIn('id',array_unique(array_column($main,'supplier')))
  568. ->pluck('title','id')
  569. ->toArray();
  570. // $depart = Depart::whereIn('id',array_unique(array_column($main,'depart_id')))
  571. // ->pluck('title','id')
  572. // ->toArray();
  573. $emp = Employee::whereIn('id',array_unique(array_column($main,'purchase_id')))
  574. ->pluck('number','id')
  575. ->toArray();
  576. $code_map = BasicType::whereIn('id',array_unique(array_column($main,'purchase_type')))
  577. ->pluck('title','id')
  578. ->toArray();
  579. $sub = PurchaseOrderInfo::whereIn('purchase_order_id',$id)
  580. ->where('del_time',0)
  581. ->get()->toArray();
  582. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  583. ->get()->toArray();
  584. $product_map = array_column($product,null,'id');
  585. $sub_map = [];
  586. foreach ($sub as $value){
  587. $product_tmp = $product_map[$value['product_id']] ?? [];
  588. $value['code'] = $product_tmp['code'];
  589. //计算金额
  590. if($value['rate'] > 0){
  591. // ipertaxrate 税率
  592. // iunitprice 原币单价
  593. // itaxprice 原币含税单价
  594. // isum 原币价税合计
  595. // imoney 原币无税金额
  596. // itax 原币税额
  597. $value['ipertaxrate'] = $value['rate'];
  598. $rate = round($value['rate'] / 100,2);
  599. $value['iunitprice'] = round($value['price'] / (1 + $rate),2);
  600. $value['itaxprice'] = $value['price'];
  601. $value['isum'] = round($value['price'] * $value['number'],2);
  602. $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  603. $value['itax'] = round($value['isum'] - $value['imoney'],2);
  604. }else{
  605. $value['ipertaxrate'] = 0;
  606. $value['iunitprice'] = $value['price'];
  607. $value['itaxprice'] = $value['price'];
  608. $value['isum'] = $value['price'] * $value['number'];
  609. $value['imoney'] = $value['isum'];
  610. $value['itax'] = 0;
  611. }
  612. $sub_map[$value['purchase_order_id']][] = $value;
  613. }
  614. foreach ($main as $key => $value){
  615. $main[$key]['cptname'] = $code_map[$value['purchase_type']] ?? "";
  616. $main[$key]['cvenname'] = $supplier[$value['supplier']] ?? "";
  617. // $main[$key]['cdepname'] = $depart[$value['depart_id']] ?? "";
  618. // $main[$key]['cpersoncode'] = $emp[$value['purchase_id']] ?? "";
  619. $main[$key]['jobnumber'] = $emp[$value['purchase_id']] ?? "";
  620. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  621. }
  622. return $main;
  623. }
  624. public function getSaleOrderData($id){
  625. $main = SalesOrder::whereIn('id',$id)
  626. ->where('del_time',0)
  627. ->get()->toArray();
  628. if(empty($main)) return [];
  629. $main_map = array_column($main,null,'id');
  630. $sub = SalesOrderProductInfo::whereIn('sales_order_id',$id)
  631. ->where('del_time',0)
  632. ->get()->toArray();
  633. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  634. ->get()->toArray();
  635. $product_map = array_column($product,null,'id');
  636. $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'))));
  637. $code_map = BasicType::whereIn('id',$code_id)
  638. ->pluck('title','id')
  639. ->toArray();
  640. $customer_map = Customer::whereIn('id',array_unique(array_column($main,'customer_id')))
  641. ->pluck('title','id')
  642. ->toArray();
  643. $empList = Employee::whereIn('id',array_unique(array_column($main,'crt_id')))
  644. ->select('number','id','emp_name')
  645. ->get()
  646. ->toArray();
  647. $emp = array_column($empList,'number','id');
  648. $emp2 = array_column($empList,'emp_name','id');
  649. $sub_map = [];
  650. foreach ($sub as $value){
  651. $product_tmp = $product_map[$value['product_id']] ?? [];
  652. $main_tmp = $main_map[$value['sales_order_id']] ?? [];
  653. $position = $code_map[$main_tmp['install_position']] ?? "";
  654. $cdefine25 = $code_map[$main_tmp['plat_type']] ?? "";//平台类型
  655. $cdefine31 = $customer_map[$main_tmp['customer_id']] ?? "";
  656. $cdefine28 = $cdefine29 = $cdefine30 = $cdefine32 = "";
  657. if($main_tmp['model_type'] == SalesOrder::Model_type_four){
  658. //线上订单
  659. $cdefine28 = $main_tmp['plat_order'] ?? "";//平台单号
  660. $cdefine29 = $main_tmp['cdefine29'] ?? "";
  661. $cdefine30 = $main_tmp['cdefine30'] ?? "";
  662. $cdefine32 = $main_tmp['cdefine32'] ?? "";
  663. }elseif($main_tmp['model_type'] == SalesOrder::Model_type_two){
  664. //分社订货
  665. $purchase_order = PurchaseOrder::where('del_time',0)
  666. ->where('order_number',$main_tmp['contact_order_no'])
  667. ->first();
  668. if(! empty($purchase_order)) $cdefine28 = SalesOrder::where('id',$purchase_order->sales_order_id)->value('order_number') ?? "";
  669. $cdefine25 = "渠道部—分社";
  670. $depart_tmp = Depart::where('id', $main_tmp['top_depart_id'])->first();
  671. if(! empty($depart_tmp)) {
  672. $depart_tmp = $depart_tmp->toArray();
  673. if($depart_tmp['channel_id'] > 0) $cdefine30 = Employee::where('id',$depart_tmp['channel_id'])->value('emp_name');
  674. }
  675. }else{
  676. $cdefine28 = $main_tmp['order_number'] ?? "";
  677. $cdefine29 = $position;
  678. $cdefine30 = $emp2[$main_tmp['crt_id']] ?? "";
  679. }
  680. // "itaxrate"=>$son['itaxrate'], //税率
  681. // "iunitprice"=>$son['iunitprice'],//原币单价
  682. // "itaxunitprice"=>$son['itaxunitprice'], // 原币含税单价
  683. // "isum"=>$son['isum'], //原币价税合计
  684. // "imoney"=>$son['imoney'], //原币无税金额
  685. // "itax"=>$son['itax'],//原币税额
  686. //计算金额
  687. //比如这4个产品合同金额是300.11,那么价税合计就是300.11,
  688. //含税单价就是300.11/4,目前加了税率的没有计算规则。税率不进入计算
  689. $value['itaxrate'] = 0;//税率
  690. $value['iunitprice'] = $value['price'];
  691. $value['itaxunitprice'] = $value['price'];
  692. $value['isum'] = $value['final_amount']; //原币价税合计
  693. $value['imoney'] = $value['isum']; //原币无税金额
  694. $value['itax'] = 0;
  695. // if($value['rate'] > 0){
  696. // $value['itaxrate'] = $value['rate'];
  697. // $rate = round($value['rate'] / 100,2);
  698. // $value['iunitprice'] = round($value['final_amount'] / (1 + $rate),2);
  699. // $value['itaxunitprice'] = $value['final_amount'];
  700. // $value['isum'] = round($value['final_amount'] * $value['number'],2);
  701. // $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  702. // $value['itax'] = round($value['isum'] - $value['imoney'],2);
  703. // }else{
  704. // $value['itaxrate'] = 0;
  705. // $value['iunitprice'] = $value['final_amount'];
  706. // $value['itaxunitprice'] = $value['final_amount'];
  707. // $value['isum'] = $value['final_amount'] * $value['number'];
  708. // $value['imoney'] = $value['isum'];
  709. // $value['itax'] = 0;
  710. // }
  711. $value['cdefine25'] = $cdefine25;//平台类型
  712. $value['cdefine28'] = $cdefine28; //平台单号
  713. $value['cdefine29'] = $cdefine29;//分社施工
  714. $value['cdefine32'] = $cdefine32;//达人昵称
  715. $value['cdefine31'] = $cdefine31;//客户名称
  716. $value['cdefine22'] = $main_tmp['customer_contact'] ?? "";//手机号码
  717. $value['cdefine30'] = $cdefine30;//业务员
  718. $value['code'] = $product_tmp['code'];//存货编码
  719. $sub_map[$value['sales_order_id']][] = $value;
  720. }
  721. foreach ($main as $key => $value){
  722. $customer_short_name = $code_map[$value['customer_short_name']] ?? "";
  723. if($value['model_type'] == SalesOrder::Model_type_two && $value['customer_short_name'] == 0) {
  724. $purchase_tmp = PurchaseOrder::where('order_number',$value['contact_order_no'])->value("top_depart_id");
  725. $customer_short_name = Depart::where('id',$purchase_tmp)->value('title');
  726. }
  727. $main[$key]['cbustype'] = "普通销售"; //业务类型(本身就是中文)
  728. $main[$key]['cstname'] = SalesOrder::$model_type_title_u8[$value['model_type']] ?? ""; //销售类型
  729. $main[$key]['ccusabbname'] = $customer_short_name;//客户简称
  730. // $main[$key]['cdepname'] = $depart[$value['top_depart_id']] ?? "";//部门名称
  731. // $main[$key]['cpersoncode'] = $emp[$value['crt_id']] ?? "";//业务员
  732. // $main[$key]['jobnumber'] = $emp[$value['crt_id']] ?? "";
  733. if($value['model_type'] == SalesOrder::Model_type_one){
  734. $main[$key]['jobnumber'] = "T90043";
  735. }elseif ($value['model_type'] == SalesOrder::Model_type_two){
  736. $main[$key]['jobnumber'] = "T90022";
  737. }elseif ($value['model_type'] == SalesOrder::Model_type_four){
  738. $main[$key]['jobnumber'] = "T90043";
  739. }else{
  740. $main[$key]['jobnumber'] = "T90000";
  741. }
  742. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  743. }
  744. return $main;
  745. }
  746. public function getMessage($id,$type){
  747. $result = U8Job::where('del_time',0)
  748. ->whereIn('data',$id)
  749. ->where('data_type',$type)
  750. ->select('data','state','crt_time','msg')
  751. ->get()->toArray();
  752. $return = [];
  753. foreach ($result as $value){
  754. if($value['state'] == 1){
  755. $return[$value['data']] = '同步成功';
  756. }else{
  757. $return[$value['data']] = '同步失败:' . $value['msg'];
  758. }
  759. }
  760. return $return;
  761. }
  762. public function post_u8_helper($url, $data, $header = [], $timeout = 50){
  763. $ch = curl_init();
  764. curl_setopt($ch, CURLOPT_URL, $url);
  765. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  766. curl_setopt($ch, CURLOPT_ENCODING, '');
  767. curl_setopt($ch, CURLOPT_POST, 1);
  768. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  769. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  770. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  771. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  772. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  773. $r = curl_exec($ch);
  774. curl_close($ch);
  775. return json_decode($r, true);
  776. }
  777. }