FyySqlServerService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BoxDetail;
  4. use App\Model\Employee;
  5. use App\Model\ErrorTable;
  6. use App\Model\Orders;
  7. use App\Model\SaleOrdersProduct;
  8. use App\Model\SalesFrom;
  9. use Illuminate\Support\Facades\Config;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Redis;
  13. class FyySqlServerService extends Service
  14. {
  15. public $db = null;
  16. public $db2 = null;
  17. public $error = null;
  18. public $host = "192.168.0.157";//数据库外网域名
  19. public $host_api = '192.168.0.157';//用友接口外网域名
  20. public $port = 1433;
  21. public $database = "UFDATA_001_2024";
  22. public $url = "";
  23. public $sAccID = "(default)@001";
  24. public $sUserID = "0001";
  25. public $sPassword = "";
  26. public function __construct($user_id = [], $is_db2 = false)
  27. {
  28. try {
  29. //用户信息校验
  30. if (empty($user_id['id'])) {
  31. $this->error = '恒成塑业数据库连接用户参数不能为空!';
  32. return;
  33. }
  34. //获取用友账号密码
  35. $emp = Employee::where('id', $user_id['id'])->select('sqlserver_account', 'sqlserver_password')->first();
  36. if (empty($emp) || empty($emp->sqlserver_account)) {
  37. $this->error = '恒成塑业连接构造失败,未找到账号对应的用友账号信息';
  38. return;
  39. }
  40. $this->host = env('Yongyou_database_ip');
  41. $this->port = env('Yongyou_database_port');
  42. $this->host_api = env('Yongyou_api_ip');
  43. $this->database = env('Yongyou_database');
  44. //映射ip是否通畅
  45. $bool = $this->isHostReachable($this->host);
  46. if(! $bool) {
  47. $this->error = $this->host . "连接不可达,请稍后重新操作!";
  48. return;
  49. }
  50. //用友接口统一登录账号密码
  51. $this->sUserID = $emp->sqlserver_account ?? '';
  52. $this->sPassword = $emp->sqlserver_password ?? '';
  53. $this->url = $this->host_api . "/U8Sys/U8API";
  54. if(!$this->db) {
  55. $config = [
  56. 'driver' => 'sqlsrv',
  57. 'host' => $this->host,
  58. 'port' => $this->port,
  59. 'database' => $this->database,
  60. 'username' => env('SQLSRV_USERNAME'),
  61. 'password' => env('SQLSRV_PASSWORD'),
  62. ];
  63. // 进行数据库连接
  64. Config::set('database.connections.sqlsrvs', $config);
  65. $pdo = DB::connection('sqlsrvs')->getPdo();
  66. if ($pdo instanceof \PDO) {
  67. // 连接成功的逻辑代码
  68. $this->db = DB::connection('sqlsrvs');
  69. } else {
  70. $this->error = '连接失败!';
  71. return;
  72. }
  73. }
  74. if($is_db2){
  75. $config = [
  76. 'driver' => 'sqlsrv',
  77. 'host' => $this->host,
  78. 'port' => $this->port,
  79. 'database' => 'UFSystem',
  80. 'username' => env('SQLSRV_USERNAME'),
  81. 'password' => env('SQLSRV_PASSWORD'),
  82. ];
  83. // 进行数据库连接
  84. Config::set('database.connections.sqlsrvs', $config);
  85. $pdo = DB::connection('sqlsrvs')->getPdo();
  86. if ($pdo instanceof \PDO) {
  87. // 连接成功的逻辑代码
  88. $this->db2 = DB::connection('sqlsrvs');
  89. } else {
  90. $this->error = '连接失败!';
  91. return;
  92. }
  93. }
  94. } catch (\Throwable $e) {
  95. $this->error = $e->getMessage();
  96. }
  97. }
  98. public function is_same_month($timestamp1, $timestamp2)
  99. {
  100. // 格式化时间戳为年份和月份
  101. $year1 = date('Y', $timestamp1);
  102. $month1 = date('m', $timestamp1);
  103. $year2 = date('Y', $timestamp2);
  104. $month2 = date('m', $timestamp2);
  105. if ($year1 === $year2 && $month1 === $month2) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. //获取数据(点击引入)
  112. public function getDataFromSqlServer($data)
  113. {
  114. if (!empty($this->error)) return [false, $this->error, ''];
  115. if (empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false, '制单日期不能为空!', ''];
  116. $bool = $this->is_same_month($data['out_order_no_time'][0], $data['out_order_no_time'][1]);
  117. if (!$bool) return [false, '制单日期必须同月!', ''];
  118. //查询产品主表副表数据
  119. $start = date('Y-m-d H:i:s.000', $data['out_order_no_time'][0]);
  120. $end = date('Y-m-d H:i:s.000', $data['out_order_no_time'][1]);
  121. $model = $this->db->table('SO_SOMain as a')
  122. ->leftJoin('SO_SODetails as b', 'b.cSOCode', 'a.cSOCode')
  123. ->whereBetween('a.dDate', [$start, $end])
  124. ->whereNotNull('a.cVerifier')
  125. ->select('a.cSOCode as out_order_no', 'a.dDate as out_order_no_time', 'a.cCusCode as customer_no', 'a.cCusName as customer_name', 'a.cMemo as table_header_mark', 'a.cMaker as out_crt_man', 'a.cVerifier as out_checker_man', 'a.dverifydate as out_checker_time', 'b.cInvCode as product_no', 'b.iQuantity as order_quantity', 'b.cDefine28 as technology_material', 'b.cFree1 as technology_name', 'b.cFree2 as wood_name', 'b.cDefine30 as process_mark', 'b.cMemo as table_body_mark', 'b.iTaxUnitPrice as price','b.dPreDate as pre_shipment_time');
  126. if (!empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%' . $data['out_order_no'] . '%');
  127. if (!empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%' . $data['customer_no'] . '%');
  128. if (!empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%' . $data['customer_name'] . '%');
  129. if (!empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%' . $data['table_header_mark'] . '%');
  130. if (!empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%' . $data['out_crt_man'] . '%');
  131. if (!empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%' . $data['out_checker_man'] . '%');
  132. if (!empty($data['out_checker_time'][0]) && !empty($data['out_checker_time'][1])) {
  133. $start1 = date('Y-m-d H:i:s.000', $data['out_checker_time'][0]);
  134. $end1 = date('Y-m-d H:i:s.000', $data['out_checker_time'][1]);
  135. $model->whereBetween('a.dverifydate', [$start1, $end1]);
  136. }
  137. if (!empty($data['pre_shipment_time'][0]) && !empty($data['pre_shipment_time'][1])) {
  138. $start1 = date('Y-m-d H:i:s.000', $data['pre_shipment_time'][0]);
  139. $end1 = date('Y-m-d H:i:s.000', $data['pre_shipment_time'][1]);
  140. $model->whereBetween('b.dPreDate', [$start1, $end1]);
  141. }
  142. $result = $model->get()->toArray();
  143. if (empty($result)) return [false, '暂无数据,更新结束!', ''];
  144. list($status, $msg) = $this->orderRule($result);
  145. if (empty($msg)) return [false, '暂无数据,更新结束!', ''];
  146. $result = $msg;
  147. //查询附带的一些信息(比较少)
  148. $product_no = array_filter(array_column($result, 'product_no'));
  149. $chunkSize = 1000; // 每个子集的大小
  150. $chunks = array_chunk($product_no, $chunkSize); // 将原始数组拆分成多个较小的子数组
  151. $results = []; // 存储查询结果的数组
  152. foreach ($chunks as $chunk) {
  153. $tmp = $this->db->table('Inventory as a')
  154. ->join('ComputationUnit as b', function ($join) {
  155. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  156. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  157. }, null, null, 'left')
  158. ->whereIn('a.cInvCode', $chunk)
  159. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  160. ->get()
  161. ->toArray();
  162. $results = array_merge($results, $tmp); // 将每个子集的结果合并到总结果数组中
  163. }
  164. $messageMap = array_column($results, null, 'product_no');
  165. unset($results);
  166. //现存量查询开始 ---组织查询条件
  167. $args = '';
  168. foreach ($result as $value) {
  169. $product = $value->product_no;
  170. $technology_name = $value->technology_name ?? '';
  171. // $wood_name = $value->wood_name ?? '';
  172. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}') OR ";
  173. }
  174. $args = rtrim($args, 'OR ');
  175. $messageTwo = $this->db->table('CurrentStock as a')
  176. ->leftJoin('Warehouse as b', 'b.cWhCode', 'a.cWhCode')
  177. ->whereRaw("($args)")
  178. ->where('a.iQuantity', '>', 0)
  179. ->select('a.iQuantity as product_quantity_on_hand', 'a.cInvCode as product_no', 'a.cFree1 as technology_name', 'a.cFree2 as wood_name', 'b.cWhName as warehouse_name')
  180. ->get()->toArray();
  181. if (!empty($messageTwo)) {
  182. foreach ($messageTwo as $key => $value) {
  183. $messageTwo[$key] = (array)$value;
  184. }
  185. }
  186. //现存量查询结束
  187. foreach ($result as $key => $value) {
  188. $result[$key]->technology_material = $value->technology_material ?? '';
  189. $result[$key]->technology_name = $value->technology_name ?? '';
  190. $result[$key]->wood_name = $value->wood_name ?? '';
  191. $result[$key]->process_mark = $value->process_mark ?? '';
  192. $result[$key]->table_body_mark = $value->table_body_mark ?? '';
  193. $result[$key]->table_header_mark = $value->table_header_mark ?? '';
  194. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  195. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  196. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  197. $result[$key]->pre_shipment_time = $value->pre_shipment_time ? strtotime($value->pre_shipment_time) : 0;
  198. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  199. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  200. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  201. $result[$key] = (array)$value;
  202. }
  203. return [true, $result, $messageTwo];
  204. }
  205. public function orderRule($data)
  206. {
  207. $result = Orders::where('del_time', 0)
  208. ->whereIn('out_order_no', array_column($data, 'out_order_no'))
  209. ->select('out_order_no')
  210. ->get()->toArray();
  211. $out_order_no = array_column($result, 'out_order_no');
  212. if (!empty($out_order_no)) {
  213. foreach ($data as $key => $value) {
  214. if (in_array($value->out_order_no, $out_order_no)) {
  215. unset($data[$key]);
  216. }
  217. }
  218. }
  219. return [true, $data];
  220. }
  221. //获取数据(刷新现存量)
  222. public function getDataFromSqlServerForOnHand($data)
  223. {
  224. if (!empty($this->error)) return [false, $this->error, ''];
  225. if (empty($data['id'])) return [false, '数据不能为空!', ''];
  226. $product = SaleOrdersProduct::whereIn('id', $data['id'])
  227. ->select('product_no', 'technology_name')
  228. ->get()->toArray();
  229. //现存量查询开始 ---组织查询条件
  230. $args = '';
  231. foreach ($product as $value) {
  232. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}') OR ";
  233. }
  234. $args = rtrim($args, 'OR ');
  235. $message = $this->db->table('CurrentStock as a')
  236. ->leftJoin('Warehouse as b', 'b.cWhCode', 'a.cWhCode')
  237. ->whereRaw("($args)")
  238. ->where('a.iQuantity', '>', 0)
  239. ->select('a.iQuantity as product_quantity_on_hand', 'a.cInvCode as product_no', 'a.cFree1 as technology_name', 'a.cFree2 as wood_name', 'b.cWhName as warehouse_name')
  240. ->get()->toArray();
  241. if (!empty($message)) {
  242. foreach ($message as $key => $value) {
  243. $message[$key] = (array)$value;
  244. }
  245. }
  246. //现存量查询结束
  247. return [true, $message, $product];
  248. }
  249. //获取用友账号的人名
  250. public function getYongyouName(){
  251. if (!empty($this->error)) return [false, $this->error, ''];
  252. $str = "";
  253. $model = $this->db->table('UA_User')
  254. ->where('cUser_Id',$this->sUserID)
  255. ->select('cUser_Name')
  256. ->first();
  257. if(! empty($model)) $str = $model->cUser_Name;
  258. return $str;
  259. }
  260. //产成品入库单保存接口以及审核
  261. public function U8Rdrecord10Save($data, $data_detail, $bredvouch = 0)
  262. {
  263. if (! empty($this->error)) return [false, $this->error];
  264. if ($bredvouch) {
  265. $cmemo = '来源:恒成塑业完工操作撤回';
  266. } else {
  267. $cmemo = '来源:恒成塑业包装操作 包装单号:' . $data['order_no'];
  268. }
  269. //数据
  270. $bodys = [];
  271. foreach ($data_detail as $value){
  272. $key = $value['ext_1'] . $value['ext_3'];
  273. if(! isset($bodys[$key])){
  274. $bodys[$key] = [
  275. "cinvcode" => $value["ext_1"],
  276. "cposition" => "",
  277. "cbatch" => "",
  278. "iquantity" => $value["num"],
  279. "inum" => $value["num"],
  280. "iunitcost" => 0,
  281. "iprice" => 0,
  282. "iinvexchrate" => 0,
  283. "impoids" => "",
  284. "cmocode" => "",
  285. "imoseq" => "",
  286. "cbmemo" => "",
  287. "cfree1" => $value['ext_3'], //颜色
  288. "cfree2" => "",
  289. "cdefine28" => "",
  290. ];
  291. }else{
  292. $bodys[$key]['iquantity'] += $value['num'];
  293. $bodys[$key]['inum'] += $value['num'];
  294. }
  295. }
  296. $bodys = array_values($bodys);
  297. $post = [
  298. "password" => "cloud@123456",
  299. "entity" => "U8Rdrecord10Save",
  300. "login" => [
  301. "sAccID" => $this->sAccID,
  302. "sDate" => date("Y-m-d"),
  303. "sServer" => '127.0.0.1',
  304. "sUserID" => $this->sUserID,
  305. "sSerial" => "",
  306. "sPassword" => $this->sPassword
  307. ],
  308. "data" => [
  309. "ccode" => '',
  310. "ddate" => date("Y-m-d"),
  311. "cmaker" => $data['create_name'],
  312. "dnmaketime" => date("Y-m-d"),
  313. "IsExamine" => true,
  314. "chandler" => $data['create_name'],
  315. "dnverifytime" => date("Y-m-d"),
  316. "bredvouch" => $bredvouch,
  317. "cwhcode" => "002",
  318. "cdepcode" => "03",
  319. "crdcode" => "102", //生产入库
  320. "cmemo" => $cmemo,
  321. "cdefine10" => $data['ext_1'] ?? "", //客户名称
  322. "bodys" => $bodys
  323. ]
  324. ];
  325. Log::channel('apiLog')->info('产成品入库:源数据', ["param" => $post]);
  326. $return = $this->post_helper($this->url, json_encode($post), ['Content-Type:application/json'],70);
  327. Log::channel('apiLog')->info('产成品入库:返回结果', ["param" => $return]);
  328. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  329. return [$return['flag'], $return['msg']];
  330. }
  331. //销售出库单保存接口给以及审核
  332. public function U8Rdrecord32Save($data,$create_name, $bredvouch = 0)
  333. {
  334. if (!empty($this->error)) return [false, $this->error];
  335. if ($bredvouch) {
  336. $cmemo = '来源:恒成塑业发货出库操作(撤回)';
  337. } else {
  338. $cmemo = '来源:恒成塑业发货出库操作';
  339. }
  340. foreach ($data as $value) {
  341. $bodys_tmp = [];
  342. foreach ($value['product'] as $v){
  343. $bodys_tmp[] = [
  344. "idlsid" => "",
  345. "cdlcode" => $value['cdlcode_string'],
  346. "dlrowno" => $v['line'],
  347. "cbdlcode" => "",
  348. "cinvcode" => $v['cinvcode'],
  349. "cposition" => "",
  350. "cbatch" => "",
  351. "iquantity" => $v['iquantity'],
  352. "inum" => 0,
  353. "iinvexchrate" => 0,
  354. "iunitcost" => 0,
  355. "iprice" => 0,
  356. "cbmemo" => "",
  357. "cfree1" => $v['cfree1'],
  358. "cfree2" => "",
  359. ];
  360. }
  361. $cmemo = $cmemo . '(发货单信息:' . $value['cdlcode_string'] . ')';
  362. $post_tmp = [
  363. "password" => "cloud@123456",
  364. "entity" => "U8Rdrecord32Save",
  365. "login" => [
  366. "sAccID" => $this->sAccID,
  367. "sDate" => date("Y-m-d"),
  368. "sServer" => '127.0.0.1',
  369. "sUserID" => $this->sUserID,
  370. "sSerial" => "",
  371. "sPassword" => $this->sPassword
  372. ],
  373. "data" => [
  374. "ccode" => '',
  375. "ddate" => date("Y-m-d"),
  376. "cmaker" => $create_name,
  377. "dnmaketime" => date("Y-m-d"),
  378. "IsExamine" => true,
  379. "chandler" => $create_name,
  380. "dnverifytime" => date("Y-m-d"),
  381. "bredvouch" => $bredvouch,
  382. "cdepcode" => "03",
  383. "ccuscode" => $value['customer_code'],
  384. "crdcode" => '202',
  385. "cmemo" => $cmemo,
  386. "cwhcode" => "002",
  387. "bodys" => $bodys_tmp,
  388. ]
  389. ];
  390. }
  391. Log::channel('apiLog')->info('销售出库单:源数据', ["param" => $post_tmp]);
  392. $return = $this->post_helper($this->url, json_encode($post_tmp), ['Content-Type:application/json'], 70);
  393. Log::channel('apiLog')->info('销售出库单:返回结果', ["param" => $return]);
  394. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  395. if (! $return['flag']) return [false, $return['msg']];
  396. return [true, ''];
  397. }
  398. public function getBoxData($data)
  399. {
  400. $boxData = BoxDetail::from('box_detail as a')
  401. ->leftJoin('sale_orders_product as b', 'b.id', 'a.top_id')
  402. ->where('a.del_time', 0)
  403. ->where('a.order_no', $data['order_number']) //包装单号
  404. ->select('a.num as iquantity', 'b.product_no as cinvcode', 'b.technology_name as cfree1', 'b.wood_name as cfree2', 'b.out_order_no as cSOCode');
  405. return $boxData;
  406. }
  407. //获取发货单数据 还没发的
  408. public function getDataFromDispatchList($data)
  409. {
  410. $model = $this->db->table('DispatchList as a')
  411. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  412. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  413. ->whereNotNull('a.cVerifier');
  414. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity');
  415. //检索条件
  416. if (!empty($data['time'][0]) && !empty($data['time'][1])) {
  417. $model->where('a.dDate', '>=', $data['time'][0]);
  418. $model->where('a.dDate', '<=', $data['time'][1]);
  419. }
  420. if (!empty($data['order_no'])) $model->where('b.cSOcode', $data['order_no']);
  421. if (!empty($data['out_order_no'])) $model->where('b.cSOcode', $data['out_order_no']);
  422. $message = $model->select('a.cDLCode as cdlcode', 'a.DLID as id', 'a.cCusName as customer_name', 'b.cSOCode as csocode', 'a.cDepCode as cdepcode', 'a.cCusCode as cuscode', 'b.iDLsID as idlsid', 'b.cWhCode as cwhcode', 'b.cInvCode as cinvcode', 'b.cInvName as product_title', 'b.cFree1 as cfree1', 'b.cFree2 as cfree2', 'b.cPosition as cposition', 'b.cBatch as cbatch', 'b.iQuantity as iquantity', 'b.iNum as inum', 'b.iInvExchRate as iinvexchrate', 'b.fOutQuantity as out_quantity', 'b.iUnitPrice as iunitcost', 'b.iMoney as imoney', 'b.cDefine28 as technology_material', 'b.cDefine30 as process_mark', 'c.cInvStd as product_size')
  423. ->get()->toArray();
  424. if (!empty($message)) {
  425. foreach ($message as $key => $value) {
  426. // $message[$key]->iquantity = $value->iquantity - $value->out_quantity;
  427. $message[$key] = (array)$value;
  428. }
  429. }
  430. return $message;
  431. }
  432. public function post_helper($url, $data, $header = [], $timeout = 60)
  433. {
  434. $ch = curl_init();
  435. curl_setopt($ch, CURLOPT_URL, $url);
  436. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  437. curl_setopt($ch, CURLOPT_ENCODING, '');
  438. curl_setopt($ch, CURLOPT_POST, 1);
  439. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  440. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  441. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  442. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  443. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  444. $r = curl_exec($ch);
  445. curl_close($ch);
  446. return json_decode($r, true);
  447. }
  448. //获取发货单数据 还没发的 分页
  449. public function getDataFromDispatchListPage($data)
  450. {
  451. //检索条件
  452. if (empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间区间不能为空!'];
  453. if(empty($data['page_size'])) $data['page_size'] = 20;
  454. if(empty($data['page_index'])) $data['page_index'] = 1;
  455. $model = $this->db->table('DispatchList as a')
  456. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  457. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  458. ->select('a.cDLCode as cdlcode', 'a.DLID as id', 'a.cCusName as customer_name', 'b.cSOCode as csocode', 'a.cDepCode as cdepcode', 'a.cCusCode as cuscode', 'a.dDate as date','b.irowno', 'b.iDLsID as idlsid', 'b.cWhCode as cwhcode', 'b.cInvCode as cinvcode', 'b.cInvName as product_title', 'b.cFree1 as cfree1', 'b.cFree2 as cfree2', 'b.cPosition as cposition', 'b.cBatch as cbatch', 'b.iQuantity as iquantity', 'b.iNum as inum', 'b.iInvExchRate as iinvexchrate', 'b.fOutQuantity as out_quantity', 'b.iUnitPrice as iunitcost', 'b.iMoney as imoney', 'b.cDefine28 as technology_material', 'b.cDefine30 as process_mark', 'c.cInvStd as product_size', DB::raw('(b.iQuantity - b.fOutQuantity) as quantity'), 'a.cMemo as table_header_mark', 'b.cMemo as table_body_mark')
  459. ->whereNotNull('a.cVerifier')
  460. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity')
  461. ->where('a.dDate', '>=', $data['time'][0])
  462. ->where('a.dDate', '<=', $data['time'][1]);
  463. if (!empty($data['cdlcode'])) $model->where('a.cDLCode', 'Like', '%' . $data['cdlcode'] . '%');
  464. if (!empty($data['cinvcode'])) $model->where('b.cInvCode', 'Like', '%' . $data['cinvcode'] . '%');
  465. if (!empty($data['product_title'])) $model->where('b.cInvName', 'Like', '%' . $data['product_title'] . '%');
  466. if (!empty($data['product_size'])) $model->where('c.cInvStd', 'Like', '%' . $data['product_size'] . '%');
  467. if (!empty($data['technology_material'])) $model->where('b.cDefine28', 'Like', '%' . $data['technology_material'] . '%');
  468. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  469. if (!empty($data['cfree2'])) $model->where('b.cFree2', 'Like', '%' . $data['cfree2'] . '%');
  470. if (!empty($data['process_mark'])) $model->where('b.cDefine30', 'Like', '%' . $data['process_mark'] . '%');
  471. $list = $this->limit($model, '', $data);
  472. if (! empty($list['data'])) {
  473. $product_no = array_unique(array_column($list['data'], 'cinvcode'));
  474. $messageMap = $this->db->table('Inventory as a')
  475. ->join('ComputationUnit as b', function ($join) {
  476. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  477. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  478. }, null, null, 'left')
  479. ->whereIn('a.cInvCode', $product_no)
  480. ->pluck('b.cComUnitName as product_unit', 'a.cInvCode')
  481. ->toArray();
  482. $list['data'] = Collect($list['data'])->map(function ($object) {
  483. return (array)$object;
  484. })->toArray();
  485. $new_data = [];
  486. foreach ($list['data'] as $value) {
  487. $unit = $messageMap[$value['cinvcode']] ?? '';
  488. $iquantity = floatval($value['iquantity']);
  489. $out_quantity = floatval($value['out_quantity']);
  490. $quantity = floatval($value['quantity']);
  491. $inum = floatval($value['inum']);
  492. $pro = [
  493. "cinvcode" => $value['cinvcode'],
  494. "product_title" => $value['product_title'],
  495. "cfree1" => $value['cfree1'],
  496. "iquantity" => $iquantity,
  497. "inum" => $inum,
  498. "out_quantity" => $out_quantity,
  499. "product_size" => $value['product_size'] ?? "",
  500. "quantity" => $quantity,
  501. "unit" => $unit,
  502. "line" => $value['irowno'],
  503. ];
  504. if(isset($new_data[$value['cdlcode']])){
  505. $new_data[$value['cdlcode']]['product'][] = $pro;
  506. }else{
  507. $new_data[$value['cdlcode']] = [
  508. "cdlcode" => $value['cdlcode'],
  509. "csocode" => $value['csocode'],
  510. "customer_code" => $value['cuscode'] ?? "",
  511. "customer_name" => $value['customer_name'] ?? "",
  512. "product" => [$pro]
  513. ];
  514. }
  515. }
  516. $list['data'] = array_values($new_data);
  517. }
  518. return $list;
  519. }
  520. //获取发货单数据 做校验
  521. public function getDataFromDispatchListForCheck($data)
  522. {
  523. $model = $this->db->table('DispatchList as a')
  524. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  525. ->select('a.cDLCode as cdlcode', 'a.cCusName as customer_name', 'b.cSOCode as csocode', 'a.cCusCode as cuscode','b.cInvCode as cinvcode', 'b.cInvName as product_title', 'b.cFree1 as cfree1','b.iQuantity as iquantity', 'b.iNum as inum', 'b.iInvExchRate as iinvexchrate', 'b.fOutQuantity as out_quantity', 'b.iUnitPrice as iunitcost', 'b.iMoney as imoney');
  526. if (!empty($data['cdlcode'])) $model->whereIn('a.cDLCode', $data['cdlcode']);
  527. //颜色
  528. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  529. $list = $model->get()->toArray();
  530. if (! empty($list)) {
  531. $list = Collect($list)->map(function ($object) {
  532. return (array)$object;
  533. })->toArray();
  534. $new_data = [];
  535. foreach ($list as $value) {
  536. $iquantity = floatval($value['iquantity']);
  537. $out_quantity = floatval($value['out_quantity']);
  538. $inum = floatval($value['inum']);
  539. $pro = [
  540. "cinvcode" => $value['cinvcode'],
  541. "product_title" => $value['product_title'],
  542. "cfree1" => $value['cfree1'],
  543. "iquantity" => $iquantity,
  544. "inum" => $inum,
  545. "out_quantity" => $out_quantity,
  546. ];
  547. if(isset($new_data[$value['cdlcode']])){
  548. $new_data[$value['cdlcode']]['product'][] = $pro;
  549. }else{
  550. $new_data[$value['cdlcode']] = [
  551. "cdlcode" => $value['cdlcode'],
  552. "csocode" => $value['csocode'],
  553. "customer_code" => $value['cuscode'] ?? "",
  554. "customer_name" => $value['customer_name'] ?? "",
  555. "product" => [$pro]
  556. ];
  557. }
  558. }
  559. $list = $new_data;
  560. }
  561. return $list;
  562. }
  563. public function recordErrorTable($msg,$user,$data,$time,$type){
  564. // 连接到指定数据库连接
  565. ErrorTable::insert([
  566. 'msg' => $msg,
  567. 'data' => json_encode($data),
  568. 'user_id' => $user['id'],
  569. 'user_operation_time' => $time,
  570. 'type' => $type,
  571. 'order_no' => $data['order_no'] ?? ""
  572. ]);
  573. }
  574. public function getStorehouseDataFromSqlServer($data)
  575. {
  576. if (!empty($this->error)) return [false, $this->error, ''];
  577. $model = $this->db->table('Warehouse as a')
  578. ->select('cWhCode as code', 'cWhName as name');
  579. if (!empty($data['code'])) $model->where('cWhCode', 'LIKE', '%' . $data['code'] . '%');
  580. if (!empty($data['name'])) $model->where('cWhName', 'LIKE', '%' . $data['name'] . '%');
  581. $list = $this->limit($model, '', $data);
  582. return $list;
  583. }
  584. //获取产品的原材料
  585. public function getProductFromSqlServer($data){
  586. if (!empty($this->error)) return [false, $this->error, ''];
  587. $return = [];
  588. $result = $this->db->table('Inventory')
  589. ->whereIn('cInvCode', $data['product_no'])
  590. ->select('cInvCode as product_code','CINVDEFINE1 as placode', 'CINVDEFINE2 as paper')
  591. ->get()
  592. ->toArray();
  593. $product_code = [];
  594. foreach ($result as $value){
  595. if(! in_array($value->placode, $product_code) && $value->placode) $product_code[] = $value->placode;
  596. if(! in_array($value->paper, $product_code) && $value->paper) $product_code[] = $value->paper;
  597. }
  598. if(! empty($product_code)){
  599. $product_list = $this->db->table('Inventory as a')
  600. ->join('ComputationUnit as b', function ($join) {
  601. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  602. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  603. }, null, null, 'left')
  604. ->whereIn('a.cInvCode', $product_code)
  605. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  606. ->get()
  607. ->toArray();
  608. $map = array_column($product_list, null, 'product_no');
  609. foreach ($result as $value){
  610. $tmp = [];
  611. if(! empty($value->placode)) $tmp[] = (array)$map[$value->placode] ?? [];
  612. if(! empty($value->paper)) $tmp[] = (array)$map[$value->paper] ?? [];
  613. $return[$value->product_code] = $tmp;
  614. }
  615. }
  616. return $return;
  617. }
  618. //获取产品的包装材料
  619. public function getProductBzFromSqlServer($data){
  620. if (!empty($this->error)) return [false, $this->error, ''];
  621. $return = [];
  622. $result = $this->db->table('Inventory')
  623. ->whereIn('cInvCode', $data['product_no'])
  624. ->select('cInvCode as product_code','CINVDEFINE3 as bz')
  625. ->get()
  626. ->toArray();
  627. $product_code = [];
  628. foreach ($result as $value){
  629. if(! in_array($value->bz, $product_code) && $value->bz) $product_code[] = $value->bz;
  630. }
  631. if(! empty($product_code)){
  632. $product_list = $this->db->table('Inventory as a')
  633. ->join('ComputationUnit as b', function ($join) {
  634. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  635. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  636. }, null, null, 'left')
  637. ->whereIn('a.cInvCode', $product_code)
  638. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  639. ->get()
  640. ->toArray();
  641. $map = array_column($product_list, null, 'product_no');
  642. foreach ($result as $value){
  643. if(isset($map[$value->bz])){
  644. $return[$value->product_code][] = (array)$map[$value->bz];
  645. }
  646. }
  647. }
  648. return $return;
  649. }
  650. //获取销售单客户来源
  651. public function getCustomerFromSqlServer1($data){
  652. if (! empty($this->error)) return [false, $this->error, ''];
  653. $result = $this->db->table('SO_SOMain')
  654. ->whereIn('cSOCode', $data['sale_order'])
  655. ->select('cCusCode as customer_no', DB::raw("count(cCusCode) as num"))
  656. ->groupBy('cCusCode')
  657. ->get()
  658. ->toArray();
  659. if(empty($result)) return [true, []];
  660. $map = [];
  661. foreach ($result as $value){
  662. $map[$value->customer_no] = $value->num;
  663. }
  664. $return = [];
  665. $customer = $this->db->table('Customer')
  666. ->whereIn('cCusCode', array_column($result,'customer_no'))
  667. ->select('cCusCode as customer_no', 'cDCCode as address')
  668. ->get()
  669. ->toArray();
  670. foreach ($customer as $value){
  671. if(empty($value->address)) continue;
  672. $num = $map[$value->customer_no];
  673. if(isset($return[$value->address])){
  674. $return[$value->address] += $num;
  675. }else{
  676. $return[$value->address] = $num;
  677. }
  678. }
  679. return [true, $return];
  680. }
  681. public function insertSaleOrderFrom1(){
  682. $time = time();
  683. $return = [];
  684. $address_map = config('address');
  685. foreach ($address_map as $value){
  686. $return[$value['value']] = 0;
  687. }
  688. try {
  689. Orders::where('del_time', 0)
  690. ->select('out_order_no')
  691. ->orderBy('id','desc')
  692. ->chunk(200, function ($records) use(&$return) {
  693. $out_order_no = [];
  694. foreach ($records as $record){
  695. $out_order_no[] = $record->out_order_no;
  696. }
  697. $sqlServerModel = new FyySqlServerService(['id' => 1, 'zt' => '001']);
  698. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer(['sale_order' => $out_order_no]);
  699. if($status){
  700. foreach ($return as $key => $value){
  701. if(isset($msg[$key])){
  702. $return[$key] += $msg[$key];
  703. }
  704. }
  705. }
  706. echo '更新中--------' . "\n";
  707. });
  708. $insert = [];
  709. foreach ($return as $key => $value){
  710. $insert[] = [
  711. 'code' => $key,
  712. 'num' => $value,
  713. 'crt_time' => $time
  714. ];
  715. }
  716. SalesFrom::where('del_time', 0)
  717. ->update(['del_time' => $time]);
  718. SalesFrom::insert($insert);
  719. echo '更新结束--------' . "\n";
  720. }catch (\Throwable $exception){
  721. echo $exception->getMessage() . "\n";
  722. }
  723. }
  724. //获取客户来源
  725. public function getCustomerFromSqlServer(){
  726. if (! empty($this->error)) return [false, $this->error, ''];
  727. $return = [];
  728. $customer = $this->db->table('Customer')
  729. ->select('cCusCode as customer_no', 'cDCCode as address')
  730. ->get()->toArray();
  731. foreach ($customer as $value){
  732. if(empty($value->address)) continue;
  733. if(isset($return[$value->address])){
  734. $return[$value->address] += 1;
  735. }else{
  736. $return[$value->address] = 1;
  737. }
  738. }
  739. return [true, $return];
  740. }
  741. public function insertSaleOrderFrom(){
  742. $time = time();
  743. $return = [];
  744. $address_map = config('address');
  745. foreach ($address_map as $value){
  746. $return[$value['value']] = 0;
  747. }
  748. try {
  749. $sqlServerModel = new FyySqlServerService(['id' => 1, 'zt' => '001']);
  750. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer();
  751. if($status){
  752. foreach ($return as $key => $value){
  753. if(isset($msg[$key])){
  754. $return[$key] += $msg[$key];
  755. }
  756. }
  757. }
  758. echo '更新中--------' . "\n";
  759. $insert = [];
  760. foreach ($return as $key => $value){
  761. $insert[] = [
  762. 'code' => $key,
  763. 'num' => $value,
  764. 'crt_time' => $time
  765. ];
  766. }
  767. SalesFrom::where('del_time', 0)
  768. ->update(['del_time' => $time]);
  769. SalesFrom::insert($insert);
  770. echo '更新结束--------' . "\n";
  771. }catch (\Throwable $exception){
  772. echo $exception->getMessage() . "\n";
  773. }
  774. }
  775. }