FyySqlServerService.php 37 KB

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