FyySqlServerService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. $out_order_no = "";
  289. $bodys = [];
  290. foreach ($data_detail as $value){
  291. if(empty($out_order_no)) $out_order_no = $value['out_order_no'] ?? "";
  292. $key = $value['ext_1'] . $value['ext_3'];
  293. if(! isset($bodys[$key])){
  294. $bodys[$key] = [
  295. "cinvcode" => $value["ext_1"],
  296. "cposition" => "",
  297. "cbatch" => "",
  298. "iquantity" => $value["num"],
  299. "inum" => $value["num"],
  300. "iunitcost" => 0,
  301. "iprice" => 0,
  302. "iinvexchrate" => 0,
  303. "impoids" => "",
  304. "cmocode" => "",
  305. "imoseq" => "",
  306. "cbmemo" => "",
  307. "cfree1" => $value['ext_3'], //颜色
  308. "cfree2" => "",
  309. "cdefine28" => "",
  310. ];
  311. }else{
  312. $bodys[$key]['iquantity'] += $value['num'];
  313. $bodys[$key]['inum'] += $value['num'];
  314. }
  315. }
  316. $bodys = array_values($bodys);
  317. $post = [
  318. "password" => "cloud@123456",
  319. "entity" => "U8Rdrecord10Save",
  320. "login" => [
  321. "sAccID" => $this->sAccID,
  322. "sDate" => date("Y-m-d"),
  323. "sServer" => '127.0.0.1',
  324. "sUserID" => $this->sUserID,
  325. "sSerial" => "",
  326. "sPassword" => $this->sPassword
  327. ],
  328. "data" => [
  329. "ccode" => '',
  330. "ddate" => date("Y-m-d"),
  331. "cmaker" => $data['create_name'],
  332. "dnmaketime" => date("Y-m-d"),
  333. "IsExamine" => true,
  334. "chandler" => $data['create_name'],
  335. "dnverifytime" => date("Y-m-d"),
  336. "bredvouch" => $bredvouch,
  337. "cwhcode" => "002",
  338. "cdepcode" => "03",
  339. "crdcode" => "102", //生产入库
  340. "cmemo" => $cmemo,
  341. "cdefine10" => $data['ext_1'] ?? "", //客户名称
  342. "cdefine11" => $out_order_no, //批次
  343. "bodys" => $bodys
  344. ]
  345. ];
  346. Log::channel('apiLog')->info('产成品入库:源数据', ["param" => $post]);
  347. $return = $this->post_helper($this->url, json_encode($post), ['Content-Type:application/json'],70);
  348. Log::channel('apiLog')->info('产成品入库:返回结果', ["param" => $return]);
  349. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  350. return [$return['flag'], $return['msg']];
  351. }
  352. //销售出库单保存接口给以及审核
  353. public function U8Rdrecord32Save($data,$out_order_no,$create_name, $bredvouch = 0)
  354. {
  355. if (!empty($this->error)) return [false, $this->error];
  356. if ($bredvouch) {
  357. $cmemo = '来源:恒成塑业发货出库操作(撤回)';
  358. } else {
  359. $cmemo = '来源:恒成塑业发货出库操作';
  360. }
  361. $bodys_tmp = [];
  362. $customer_code = "";
  363. foreach ($data as $value) {
  364. foreach ($value['product'] as $v){
  365. $bodys_tmp[] = [
  366. "idlsid" => "",
  367. "cdlcode" => $value['cdlcode_string'],
  368. "dlrowno" => $v['line'],
  369. "cbdlcode" => "",
  370. "cinvcode" => $v['cinvcode'],
  371. "cposition" => "",
  372. "cbatch" => "",
  373. "iquantity" => $v['iquantity'],
  374. "inum" => 0,
  375. "iinvexchrate" => 0,
  376. "iunitcost" => 0,
  377. "iprice" => 0,
  378. "cbmemo" => "",
  379. "cfree1" => $v['cfree1'],
  380. "cfree2" => "",
  381. ];
  382. }
  383. $cmemo = $cmemo . '(发货单信息:' . $value['cdlcode_string'] . ')';
  384. if(empty($customer_code)) $customer_code = $value['customer_code'];
  385. }
  386. $post_tmp = [
  387. "password" => "cloud@123456",
  388. "entity" => "U8Rdrecord32Save",
  389. "login" => [
  390. "sAccID" => $this->sAccID,
  391. "sDate" => date("Y-m-d"),
  392. "sServer" => '127.0.0.1',
  393. "sUserID" => $this->sUserID,
  394. "sSerial" => "",
  395. "sPassword" => $this->sPassword
  396. ],
  397. "data" => [
  398. "ccode" => '',
  399. "ddate" => date("Y-m-d"),
  400. "cmaker" => $create_name,
  401. "dnmaketime" => date("Y-m-d"),
  402. "IsExamine" => true,
  403. "chandler" => $create_name,
  404. "dnverifytime" => date("Y-m-d"),
  405. "bredvouch" => $bredvouch,
  406. "cdepcode" => "03",
  407. "ccuscode" => $customer_code,
  408. "crdcode" => '202',
  409. "cmemo" => $cmemo,
  410. "cwhcode" => "002",
  411. "cdefine11" => $out_order_no, //批次
  412. "bodys" => $bodys_tmp,
  413. ]
  414. ];
  415. Log::channel('apiLog')->info('销售出库单:源数据', ["param" => $post_tmp]);
  416. $return = $this->post_helper($this->url, json_encode($post_tmp), ['Content-Type:application/json'], 70);
  417. Log::channel('apiLog')->info('销售出库单:返回结果', ["param" => $return]);
  418. if (empty($return)) return [false, '异常错误,请确认请求接口地址!'];
  419. if (! $return['flag']) return [false, $return['msg']];
  420. return [true, ''];
  421. }
  422. public function getBoxData($data)
  423. {
  424. $boxData = BoxDetail::from('box_detail as a')
  425. ->leftJoin('sale_orders_product as b', 'b.id', 'a.top_id')
  426. ->where('a.del_time', 0)
  427. ->where('a.order_no', $data['order_number']) //包装单号
  428. ->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');
  429. return $boxData;
  430. }
  431. //获取发货单数据 还没发的
  432. public function getDataFromDispatchList($data)
  433. {
  434. $model = $this->db->table('DispatchList as a')
  435. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  436. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  437. ->whereNotNull('a.cVerifier');
  438. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity');
  439. //检索条件
  440. if (!empty($data['time'][0]) && !empty($data['time'][1])) {
  441. $model->where('a.dDate', '>=', $data['time'][0]);
  442. $model->where('a.dDate', '<=', $data['time'][1]);
  443. }
  444. if (!empty($data['order_no'])) $model->where('b.cSOcode', $data['order_no']);
  445. if (!empty($data['out_order_no'])) $model->where('b.cSOcode', $data['out_order_no']);
  446. $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')
  447. ->get()->toArray();
  448. if (!empty($message)) {
  449. foreach ($message as $key => $value) {
  450. // $message[$key]->iquantity = $value->iquantity - $value->out_quantity;
  451. $message[$key] = (array)$value;
  452. }
  453. }
  454. return $message;
  455. }
  456. public function post_helper($url, $data, $header = [], $timeout = 60)
  457. {
  458. $ch = curl_init();
  459. curl_setopt($ch, CURLOPT_URL, $url);
  460. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  461. curl_setopt($ch, CURLOPT_ENCODING, '');
  462. curl_setopt($ch, CURLOPT_POST, 1);
  463. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  464. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  465. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  466. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  467. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  468. $r = curl_exec($ch);
  469. curl_close($ch);
  470. return json_decode($r, true);
  471. }
  472. //获取发货单数据 还没发的 分页
  473. public function getDataFromDispatchListPage($data)
  474. {
  475. //检索条件
  476. if (empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间区间不能为空!'];
  477. if(empty($data['page_size'])) $data['page_size'] = 20;
  478. if(empty($data['page_index'])) $data['page_index'] = 1;
  479. $model = $this->db->table('DispatchList as a')
  480. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  481. ->leftJoin('Inventory as c', 'c.cInvCode', 'b.cInvCode')
  482. ->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')
  483. ->whereNotNull('a.cVerifier')
  484. // ->whereColumn('b.iQuantity', '>', 'b.fOutQuantity')
  485. ->where('a.dDate', '>=', $data['time'][0])
  486. ->where('a.dDate', '<=', $data['time'][1]);
  487. if (!empty($data['cdlcode'])) $model->where('a.cDLCode', 'Like', '%' . $data['cdlcode'] . '%');
  488. if (!empty($data['cinvcode'])) $model->where('b.cInvCode', 'Like', '%' . $data['cinvcode'] . '%');
  489. if (!empty($data['product_title'])) $model->where('b.cInvName', 'Like', '%' . $data['product_title'] . '%');
  490. if (!empty($data['product_size'])) $model->where('c.cInvStd', 'Like', '%' . $data['product_size'] . '%');
  491. if (!empty($data['technology_material'])) $model->where('b.cDefine28', 'Like', '%' . $data['technology_material'] . '%');
  492. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  493. if (!empty($data['cfree2'])) $model->where('b.cFree2', 'Like', '%' . $data['cfree2'] . '%');
  494. if (!empty($data['process_mark'])) $model->where('b.cDefine30', 'Like', '%' . $data['process_mark'] . '%');
  495. $list = $this->limit($model, '', $data);
  496. if (! empty($list['data'])) {
  497. $product_no = array_unique(array_column($list['data'], 'cinvcode'));
  498. $messageMap = $this->db->table('Inventory as a')
  499. ->join('ComputationUnit as b', function ($join) {
  500. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  501. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  502. }, null, null, 'left')
  503. ->whereIn('a.cInvCode', $product_no)
  504. ->pluck('b.cComUnitName as product_unit', 'a.cInvCode')
  505. ->toArray();
  506. $list['data'] = Collect($list['data'])->map(function ($object) {
  507. return (array)$object;
  508. })->toArray();
  509. $new_data = [];
  510. foreach ($list['data'] as $value) {
  511. $unit = $messageMap[$value['cinvcode']] ?? '';
  512. $iquantity = floatval($value['iquantity']);
  513. $out_quantity = floatval($value['out_quantity']);
  514. $quantity = floatval($value['quantity']);
  515. $inum = floatval($value['inum']);
  516. $pro = [
  517. "cinvcode" => $value['cinvcode'],
  518. "product_title" => $value['product_title'],
  519. "cfree1" => $value['cfree1'],
  520. "iquantity" => $iquantity,
  521. "inum" => $inum,
  522. "out_quantity" => $out_quantity,
  523. "product_size" => $value['product_size'] ?? "",
  524. "quantity" => $quantity,
  525. "unit" => $unit,
  526. "line" => $value['irowno'],
  527. ];
  528. if(isset($new_data[$value['cdlcode']])){
  529. $new_data[$value['cdlcode']]['product'][] = $pro;
  530. }else{
  531. $new_data[$value['cdlcode']] = [
  532. "cdlcode" => $value['cdlcode'],
  533. "csocode" => $value['csocode'],
  534. "customer_code" => $value['cuscode'] ?? "",
  535. "customer_name" => $value['customer_name'] ?? "",
  536. "product" => [$pro]
  537. ];
  538. }
  539. }
  540. $list['data'] = array_values($new_data);
  541. }
  542. return $list;
  543. }
  544. //获取发货单数据 做校验
  545. public function getDataFromDispatchListForCheck($data)
  546. {
  547. $model = $this->db->table('DispatchList as a')
  548. ->leftJoin('DispatchLists as b', 'b.DLID', 'a.DLID')
  549. ->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');
  550. if (!empty($data['cdlcode'])) $model->whereIn('a.cDLCode', $data['cdlcode']);
  551. //颜色
  552. if (!empty($data['cfree1'])) $model->where('b.cFree1', 'Like', '%' . $data['cfree1'] . '%');
  553. $list = $model->get()->toArray();
  554. if (! empty($list)) {
  555. $list = Collect($list)->map(function ($object) {
  556. return (array)$object;
  557. })->toArray();
  558. $new_data = [];
  559. foreach ($list as $value) {
  560. $iquantity = floatval($value['iquantity']);
  561. $out_quantity = floatval($value['out_quantity']);
  562. $inum = floatval($value['inum']);
  563. $pro = [
  564. "cinvcode" => $value['cinvcode'],
  565. "product_title" => $value['product_title'],
  566. "cfree1" => $value['cfree1'],
  567. "iquantity" => $iquantity,
  568. "inum" => $inum,
  569. "out_quantity" => $out_quantity,
  570. ];
  571. if(isset($new_data[$value['cdlcode']])){
  572. $new_data[$value['cdlcode']]['product'][] = $pro;
  573. }else{
  574. $new_data[$value['cdlcode']] = [
  575. "cdlcode" => $value['cdlcode'],
  576. "csocode" => $value['csocode'],
  577. "customer_code" => $value['cuscode'] ?? "",
  578. "customer_name" => $value['customer_name'] ?? "",
  579. "product" => [$pro]
  580. ];
  581. }
  582. }
  583. $list = $new_data;
  584. }
  585. return $list;
  586. }
  587. public function recordErrorTable($msg,$user,$data,$time,$type){
  588. // 连接到指定数据库连接
  589. ErrorTable::insert([
  590. 'msg' => $msg,
  591. 'data' => json_encode($data),
  592. 'user_id' => $user['id'],
  593. 'user_operation_time' => $time,
  594. 'type' => $type,
  595. 'order_no' => $data['order_no'] ?? ""
  596. ]);
  597. }
  598. public function getStorehouseDataFromSqlServer($data)
  599. {
  600. if (!empty($this->error)) return [false, $this->error, ''];
  601. $model = $this->db->table('Warehouse as a')
  602. ->select('cWhCode as code', 'cWhName as name');
  603. if (!empty($data['code'])) $model->where('cWhCode', 'LIKE', '%' . $data['code'] . '%');
  604. if (!empty($data['name'])) $model->where('cWhName', 'LIKE', '%' . $data['name'] . '%');
  605. $list = $this->limit($model, '', $data);
  606. return $list;
  607. }
  608. //获取产品的原材料
  609. public function getProductFromSqlServer($data){
  610. if (!empty($this->error)) return [false, $this->error, ''];
  611. $return = [];
  612. $result = $this->db->table('Inventory')
  613. ->whereIn('cInvCode', $data['product_no'])
  614. ->select('cInvCode as product_code','CINVDEFINE1 as placode', 'CINVDEFINE2 as paper')
  615. ->get()
  616. ->toArray();
  617. $product_code = [];
  618. foreach ($result as $value){
  619. if(! in_array($value->placode, $product_code) && $value->placode) $product_code[] = $value->placode;
  620. if(! in_array($value->paper, $product_code) && $value->paper) $product_code[] = $value->paper;
  621. }
  622. if(! empty($product_code)){
  623. $product_list = $this->db->table('Inventory as a')
  624. ->join('ComputationUnit as b', function ($join) {
  625. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  626. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  627. }, null, null, 'left')
  628. ->whereIn('a.cInvCode', $product_code)
  629. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  630. ->get()
  631. ->toArray();
  632. $map = array_column($product_list, null, 'product_no');
  633. foreach ($result as $value){
  634. $tmp = [];
  635. if(! empty($value->placode)) $tmp[] = (array)$map[$value->placode] ?? [];
  636. if(! empty($value->paper)) $tmp[] = (array)$map[$value->paper] ?? [];
  637. $return[$value->product_code] = $tmp;
  638. }
  639. }
  640. return $return;
  641. }
  642. //获取产品的包装材料
  643. public function getProductBzFromSqlServer($data){
  644. if (!empty($this->error)) return [false, $this->error, ''];
  645. $return = [];
  646. $result = $this->db->table('Inventory')
  647. ->whereIn('cInvCode', $data['product_no'])
  648. ->select('cInvCode as product_code','CINVDEFINE3 as bz')
  649. ->get()
  650. ->toArray();
  651. $product_code = [];
  652. foreach ($result as $value){
  653. if(! in_array($value->bz, $product_code) && $value->bz) $product_code[] = $value->bz;
  654. }
  655. if(! empty($product_code)){
  656. $product_list = $this->db->table('Inventory as a')
  657. ->join('ComputationUnit as b', function ($join) {
  658. $join->on('a.cGroupCode', '=', 'b.cGroupCode')
  659. ->on('a.cComUnitCode', '=', 'b.cComUnitCode');
  660. }, null, null, 'left')
  661. ->whereIn('a.cInvCode', $product_code)
  662. ->select('a.cInvCode as product_no', 'a.cInvName as product_title', 'a.cInvStd as product_size', 'b.cComUnitName as product_unit')
  663. ->get()
  664. ->toArray();
  665. $map = array_column($product_list, null, 'product_no');
  666. foreach ($result as $value){
  667. if(isset($map[$value->bz])){
  668. $return[$value->product_code][] = (array)$map[$value->bz];
  669. }
  670. }
  671. }
  672. return $return;
  673. }
  674. //获取销售单客户来源
  675. public function getCustomerFromSqlServer1($data){
  676. if (! empty($this->error)) return [false, $this->error, ''];
  677. $result = $this->db->table('SO_SOMain')
  678. ->whereIn('cSOCode', $data['sale_order'])
  679. ->select('cCusCode as customer_no', DB::raw("count(cCusCode) as num"))
  680. ->groupBy('cCusCode')
  681. ->get()
  682. ->toArray();
  683. if(empty($result)) return [true, []];
  684. $map = [];
  685. foreach ($result as $value){
  686. $map[$value->customer_no] = $value->num;
  687. }
  688. $return = [];
  689. $customer = $this->db->table('Customer')
  690. ->whereIn('cCusCode', array_column($result,'customer_no'))
  691. ->select('cCusCode as customer_no', 'cDCCode as address')
  692. ->get()
  693. ->toArray();
  694. foreach ($customer as $value){
  695. if(empty($value->address)) continue;
  696. $num = $map[$value->customer_no];
  697. if(isset($return[$value->address])){
  698. $return[$value->address] += $num;
  699. }else{
  700. $return[$value->address] = $num;
  701. }
  702. }
  703. return [true, $return];
  704. }
  705. public function insertSaleOrderFrom1(){
  706. $time = time();
  707. $return = [];
  708. $address_map = config('address');
  709. foreach ($address_map as $value){
  710. $return[$value['value']] = 0;
  711. }
  712. try {
  713. Orders::where('del_time', 0)
  714. ->select('out_order_no')
  715. ->orderBy('id','desc')
  716. ->chunk(200, function ($records) use(&$return) {
  717. $out_order_no = [];
  718. foreach ($records as $record){
  719. $out_order_no[] = $record->out_order_no;
  720. }
  721. $sqlServerModel = new FyySqlServerService(['id' => 1, 'zt' => '001']);
  722. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer(['sale_order' => $out_order_no]);
  723. if($status){
  724. foreach ($return as $key => $value){
  725. if(isset($msg[$key])){
  726. $return[$key] += $msg[$key];
  727. }
  728. }
  729. }
  730. echo '更新中--------' . "\n";
  731. });
  732. $insert = [];
  733. foreach ($return as $key => $value){
  734. $insert[] = [
  735. 'code' => $key,
  736. 'num' => $value,
  737. 'crt_time' => $time
  738. ];
  739. }
  740. SalesFrom::where('del_time', 0)
  741. ->update(['del_time' => $time]);
  742. SalesFrom::insert($insert);
  743. echo '更新结束--------' . "\n";
  744. }catch (\Throwable $exception){
  745. echo $exception->getMessage() . "\n";
  746. }
  747. }
  748. //获取客户来源
  749. public function getCustomerFromSqlServer(){
  750. if (! empty($this->error)) return [false, $this->error, ''];
  751. $return = [];
  752. $customer = $this->db->table('Customer')
  753. ->select('cCusCode as customer_no', 'cDCCode as address')
  754. ->get()->toArray();
  755. foreach ($customer as $value){
  756. if(empty($value->address)) continue;
  757. if(isset($return[$value->address])){
  758. $return[$value->address] += 1;
  759. }else{
  760. $return[$value->address] = 1;
  761. }
  762. }
  763. return [true, $return];
  764. }
  765. public function insertSaleOrderFrom(){
  766. $time = time();
  767. $return = [];
  768. $address_map = config('address');
  769. foreach ($address_map as $value){
  770. $return[$value['value']] = 0;
  771. }
  772. try {
  773. $sqlServerModel = new FyySqlServerService(['id' => 1, 'zt' => '001']);
  774. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer();
  775. if($status){
  776. foreach ($return as $key => $value){
  777. if(isset($msg[$key])){
  778. $return[$key] += $msg[$key];
  779. }
  780. }
  781. }
  782. echo '更新中--------' . "\n";
  783. $insert = [];
  784. foreach ($return as $key => $value){
  785. $insert[] = [
  786. 'code' => $key,
  787. 'num' => $value,
  788. 'crt_time' => $time
  789. ];
  790. }
  791. SalesFrom::where('del_time', 0)
  792. ->update(['del_time' => $time]);
  793. SalesFrom::insert($insert);
  794. echo '更新结束--------' . "\n";
  795. }catch (\Throwable $exception){
  796. echo $exception->getMessage() . "\n";
  797. }
  798. }
  799. }