FyySqlServerService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Orders;
  4. use App\Model\SaleOrdersProduct;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Redis;
  9. class FyySqlServerService extends Service
  10. {
  11. public $db = null;
  12. public $error = null;
  13. public $host = "";
  14. public $port = 0;
  15. public $database = "";
  16. public $url = "";
  17. public function __construct()
  18. {
  19. $fyy_array = Redis::get('fyy_sql_server');
  20. if(empty($fyy_array)) {
  21. $this->error = 'SQLSERVER设置不存在!';
  22. return;
  23. }
  24. $fyy_array = json_decode($fyy_array,true);
  25. $this->host = $fyy_array['sqlserver_host'] ?? '';
  26. $this->port = $fyy_array['sqlserver_port'] ?? 0;
  27. $this->database = $fyy_array['sqlserver_database'] ?? '';
  28. $this->url = empty($this->host)?: "http://" . $this->host . ":12365/U8Sys/U8API";
  29. if(! $this->db){
  30. $config = [
  31. 'driver' => 'sqlsrv',
  32. 'host' => $fyy_array['sqlserver_host'] ?? '',
  33. 'port' => $fyy_array['sqlserver_port'] ?? '',
  34. 'database' => $fyy_array['sqlserver_database'] ?? '',
  35. 'username' => env('SQLSRV_USERNAME'),
  36. 'password' => env('SQLSRV_PASSWORD'),
  37. ];
  38. // 进行数据库连接
  39. Config::set('database.connections.sqlsrvs', $config);
  40. try {
  41. $pdo = DB::connection('sqlsrvs')->getPdo();
  42. if ($pdo instanceof \PDO) {
  43. // 连接成功的逻辑代码
  44. $this->db = DB::connection('sqlsrvs');
  45. } else {
  46. $this->error = '连接失败!';
  47. return;
  48. }
  49. } catch (\Throwable $e) {
  50. $this->error = $e->getMessage();
  51. }
  52. }
  53. }
  54. private function fakeData(){
  55. $return = [
  56. [
  57. 'out_order_no' => '001',
  58. 'out_order_no_time' => 1685928995,
  59. 'customer_no' => '201603012',
  60. 'customer_name' => '巴大胡',
  61. 'table_header_mark' => '表头备注',
  62. 'product_no' => '产品编码001',
  63. 'product_title' => '产品名称001',
  64. 'product_size' => '产品规格30c',
  65. 'product_unit' => '产品单位米',
  66. 'order_quantity' => 100,
  67. 'product_quantity_on_hand' => 50,
  68. 'technology_material' => '工艺材质',
  69. 'technology_name' => '工艺名称:同命',
  70. 'wood_name' => '木皮名称:梭梭树皮',
  71. 'process_mark' => '加工备注',
  72. 'table_body_mark' => '表体备注',
  73. 'out_crt_man' => '外部人员一',
  74. 'out_checker_man' => '外部人员二',
  75. 'out_checker_time' => 1685928995,
  76. ],
  77. [
  78. 'out_order_no' => '001',
  79. 'out_order_no_time' => 1685928995,
  80. 'customer_no' => '201603012',
  81. 'customer_name' => '巴大胡',
  82. 'table_header_mark' => '表头备注',
  83. 'product_no' => '产品编码002',
  84. 'product_title' => '产品名称002',
  85. 'product_size' => '产品规格30d',
  86. 'product_unit' => '产品单位米',
  87. 'order_quantity' => 100,
  88. 'product_quantity_on_hand' => 50,
  89. 'technology_material' => '工艺材质',
  90. 'technology_name' => '工艺名称:同命',
  91. 'wood_name' => '木皮名称:梭梭树皮',
  92. 'process_mark' => '加工备注',
  93. 'table_body_mark' => '表体备注',
  94. 'out_crt_man' => '外部人员一',
  95. 'out_checker_man' => '外部人员二',
  96. 'out_checker_time' => 1685928995,
  97. ],
  98. [
  99. 'out_order_no' => '002',
  100. 'out_order_no_time' => 1685928995,
  101. 'customer_no' => '201603012',
  102. 'customer_name' => '巴大胡',
  103. 'table_header_mark' => '表头备注',
  104. 'product_no' => '产品编码002',
  105. 'product_title' => '产品名称002',
  106. 'product_size' => '产品规格30d',
  107. 'product_unit' => '产品单位米',
  108. 'order_quantity' => 100,
  109. 'product_quantity_on_hand' => 50,
  110. 'technology_material' => '工艺材质',
  111. 'technology_name' => '工艺名称:同命',
  112. 'wood_name' => '木皮名称:梭梭树皮',
  113. 'process_mark' => '加工备注',
  114. 'table_body_mark' => '表体备注',
  115. 'out_crt_man' => '外部人员一',
  116. 'out_checker_man' => '外部人员二',
  117. 'out_checker_time' => 1685928995,
  118. ],
  119. ];
  120. return $return;
  121. }
  122. public function is_same_month($timestamp1,$timestamp2){
  123. date_default_timezone_set("PRC");
  124. // 格式化时间戳为年份和月份
  125. $year1 = date('Y', $timestamp1);
  126. $month1 = date('m', $timestamp1);
  127. $year2 = date('Y', $timestamp2);
  128. $month2 = date('m', $timestamp2);
  129. if ($year1 === $year2 && $month1 === $month2) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. //获取数据(点击引入)
  136. public function getDataFromSqlServer($data){
  137. if(! empty($this->error)) return [false,$this->error,''];
  138. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期不能为空!',''];
  139. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  140. if(! $bool) return [false,'制单日期必须同月!',''];
  141. //查询产品主表副表数据
  142. date_default_timezone_set("PRC");
  143. $start = date('Y-m-d H:i:s.000',$data['out_order_no_time'][0]);
  144. $end = date('Y-m-d H:i:s.000',$data['out_order_no_time'][1]);
  145. $model = $this->db->table('SO_SOMain as a')
  146. ->leftJoin('SO_SODetails as b','b.cSOCode','a.cSOCode')
  147. ->whereBetween('a.dDate',[$start, $end])
  148. ->whereNotNull('a.cVerifier')
  149. ->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');
  150. if(! empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%'.$data['out_order_no'].'%');
  151. if(! empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%'.$data['customer_no'].'%');
  152. if(! empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%'.$data['customer_name'].'%');
  153. if(! empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%'.$data['table_header_mark'].'%');
  154. if(! empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%'.$data['out_crt_man'].'%');
  155. if(! empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%'.$data['out_checker_man'].'%');
  156. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) {
  157. $start1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][0]);
  158. $end1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][1]);
  159. $model->whereBetween('a.dverifydate',[$start1, $end1]);
  160. }
  161. $result = $model->get()->toArray();
  162. if(empty($result)) return [false,'暂无数据,更新结束!',''];
  163. list($status,$msg) = $this->orderRule($result);
  164. if(empty($msg)) return [false,'暂无数据,更新结束!',''];
  165. $result = $msg;
  166. //查询附带的一些信息(比较少)
  167. $product_no = array_column($result,'product_no');
  168. $messageOne = $this->db->table('Inventory as a')
  169. ->join('ComputationUnit as b',function($join){
  170. $join->on('a.cGroupCode','=','b.cGroupCode')
  171. ->on('a.cComUnitCode','=','b.cComUnitCode');
  172. }, null,null,'left')
  173. ->whereIn('a.cInvCode',$product_no)
  174. ->select('a.cInvCode as product_no','a.cInvName as product_title','a.cInvStd as product_size','b.cComUnitName as product_unit')
  175. ->get()->toArray();
  176. $messageMap = array_column($messageOne,null,'product_no');
  177. //现存量查询开始 ---组织查询条件
  178. $args = '';
  179. foreach ($result as $value){
  180. $product = $value->product_no;
  181. $technology_name = $value->technology_name ?? '';
  182. $wood_name = $value->wood_name ?? '';
  183. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}' and a.cFree2 = '{$wood_name}') OR ";
  184. }
  185. $args = rtrim($args,'OR ');
  186. $messageTwo = $this->db->table('CurrentStock as a')
  187. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  188. ->whereRaw("($args)")
  189. ->where('a.iQuantity','>',0)
  190. ->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')
  191. ->get()->toArray();
  192. if(! empty($messageTwo)){
  193. foreach ($messageTwo as $key => $value){
  194. $messageTwo[$key] = (array)$value;
  195. }
  196. }
  197. //现存量查询结束
  198. foreach ($result as $key => $value){
  199. $result[$key]->technology_material = $value->technology_material ?? '';
  200. $result[$key]->technology_name = $value->technology_name ?? '';
  201. $result[$key]->wood_name = $value->wood_name ?? '';
  202. $result[$key]->process_mark = $value->process_mark ?? '';
  203. $result[$key]->table_body_mark = $value->table_body_mark ?? '';
  204. $result[$key]->table_header_mark = $value->table_header_mark ?? '';
  205. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  206. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  207. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  208. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  209. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  210. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  211. $result[$key] = (array)$value;
  212. }
  213. return [true,$result,$messageTwo];
  214. }
  215. public function orderRule($data){
  216. $result = Orders::where('del_time',0)
  217. ->whereIn('out_order_no',array_column($data,'out_order_no'))
  218. ->select('out_order_no')
  219. ->get()->toArray();
  220. $out_order_no = array_column($result,'out_order_no');
  221. if(! empty($out_order_no)) {
  222. foreach ($data as $key => $value){
  223. if(in_array($value->out_order_no,$out_order_no)){
  224. unset($data[$key]);
  225. }
  226. }
  227. }
  228. return [true,$data];
  229. }
  230. //获取数据(刷新现存量)
  231. public function getDataFromSqlServerForOnHand($data){
  232. if(! empty($this->error)) return [false,$this->error,''];
  233. if(empty($data['id'])) return [false,'数据不能为空!',''];
  234. $product = SaleOrdersProduct::whereIn('id',$data['id'])
  235. ->select('product_no','technology_name','wood_name')
  236. ->get()->toArray();
  237. //现存量查询开始 ---组织查询条件
  238. $args = '';
  239. foreach ($product as $value){
  240. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}' and a.cFree2 = '{$value['wood_name']}') OR ";
  241. }
  242. $args = rtrim($args,'OR ');
  243. $message = $this->db->table('CurrentStock as a')
  244. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  245. ->whereRaw("($args)")
  246. ->where('a.iQuantity','>',0)
  247. ->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')
  248. ->get()->toArray();
  249. if(! empty($message)){
  250. foreach ($message as $key => $value){
  251. $message[$key] = (array)$value;
  252. }
  253. }
  254. //现存量查询结束
  255. return [true,$message,$product];
  256. }
  257. //产成品入库单保存接口以及审核
  258. public function U8Rdrecord10Save($data,$bredvouch = 0){
  259. if(! empty($this->error)) return [false,$this->error];
  260. date_default_timezone_set("PRC");
  261. list($bool,$msg) = $this->createOrderNumberAboutSCRK();
  262. if(! $bool) return [false,$msg];
  263. if($bredvouch){
  264. $cmemo = '来源:福羊羊完工操作撤回';
  265. }else{
  266. $cmemo = '来源:福羊羊完工操作 派工单号:'. $data['dispatch_no'];
  267. }
  268. //数据
  269. $bodys[] = [
  270. "cinvcode" => $data["product_no"],
  271. "cposition" => "",
  272. "cbatch" => "",
  273. "iquantity" => $data["quantity"],
  274. "inum" => $data["quantity"],
  275. "iunitcost" => $data["price"] * 0.95,
  276. "iprice" => $data["price"] * 0.95 * $data['quantity'],
  277. "iinvexchrate" => "1.00",
  278. "impoids" => "",
  279. "cmocode" => "",
  280. "imoseq" => "",
  281. "cbmemo" => "",
  282. "cfree1" => "",
  283. "cfree2" => "",
  284. ];
  285. $post = [
  286. "password"=>"cloud@123456",
  287. "entity"=>"U8Rdrecord10Save",
  288. "login"=>[
  289. "sAccID"=> "(default)@001",
  290. "sDate"=> date("Y-m-d"),
  291. "sServer"=> '127.0.0.1',
  292. "sUserID"=> "董晓磊",
  293. "sSerial"=> "",
  294. "sPassword"=> ""
  295. ],
  296. "data"=>[
  297. "ccode"=>'',
  298. "ddate"=>date("Y-m-d"),
  299. "cmaker"=>"董晓磊",
  300. "dnmaketime"=> date("Y-m-d"),
  301. "IsExamine"=>true,
  302. "bredvouch"=> $bredvouch,
  303. "cwhcode"=>"02",
  304. "cdepcode"=>"06",
  305. "crdcode"=>"102", //生产入库
  306. "cmemo"=> $cmemo,
  307. "cdefine10" => $data['customer_name'], //客户名称
  308. "bodys"=>$bodys
  309. ]
  310. ];
  311. $return = $this->post_helper($this->url,json_encode($post), ['Content-Type:application/json']);
  312. return [$return['flag'], $return['msg']];
  313. }
  314. //销售出库单保存接口给以及审核
  315. public function U8Rdrecord32Save($data,$bredvouch = 0){
  316. if(! empty($this->error)) return [false,$this->error];
  317. date_default_timezone_set("PRC");
  318. $return = $this->getDataFromDispatchList($data);
  319. if(empty($return)) return [false,'根据销售订单号:'. $data['order_number'] . '未找到发货单数据!'];
  320. $main = $return[0];
  321. if($bredvouch){
  322. $cmemo = '备注一';
  323. }else{
  324. $cmemo = '销售单号:'.$main['out_order'];
  325. }
  326. //数据
  327. $bodys = [];
  328. foreach ($return as $value){
  329. $bodys[] = [
  330. "idlsid"=>$value['idlsid'],
  331. "cdlcode"=>"",
  332. "dlrowno"=>"",
  333. "cbdlcode"=>"",
  334. "cinvcode"=>$value['cinvcode'],
  335. "cposition"=>$value['cposition'],
  336. "cbatch"=>$value['cbatch'],
  337. "iquantity"=>$value['iquantity'],
  338. "inum"=>$value['inum'],
  339. "iinvexchrate"=>$value['iinvexchrate'],
  340. "iunitcost"=>"",
  341. "iprice"=>"",
  342. "cbmemo"=>"",
  343. ];
  344. }
  345. $post = [
  346. "password"=>"cloud@123456",
  347. "entity"=>"U8Rdrecord32Save",
  348. "login"=>[
  349. "sAccID"=> "(default)@001",
  350. "sDate"=> date("Y-m-d"),
  351. "sServer"=> '127.0.0.1',
  352. "sUserID"=> "董晓磊",
  353. "sSerial"=> "",
  354. "sPassword"=> ""
  355. ],
  356. "data"=>[
  357. "ccode"=>'',
  358. "ddate"=>date("Y-m-d"),
  359. "cmaker"=>"董晓磊",
  360. "dnmaketime"=> date("Y-m-d"),
  361. "IsExamine"=>true,
  362. "bredvouch"=> $bredvouch,
  363. "cwhcode"=>"02",
  364. "cdepcode"=>"06",
  365. "crdcode"=>"102",
  366. "cmemo"=> $cmemo,
  367. "cdefine10" => $main['customer_name'], //客户名称
  368. "bodys"=>$bodys
  369. ]
  370. ];
  371. $return = $this->post_helper($this->url,json_encode($post), ['Content-Type:application/json']);
  372. return [$return['flag'], $return['msg']];
  373. }
  374. public function getDataFromDispatchList($data){
  375. $message = $this->db->table('DispatchList as a')
  376. ->leftJoin('DispatchLists as b','b.DLID','a.DLID')
  377. ->where('a.cSOcode',$data['order_number'])
  378. ->select('a.cDefine10 as customer_name','a.cSOCode as out_order','b.iDLsID as idlsid','b.cInvCode as cinvcode','b.cPosition as cposition','b.cBatch as cbatch','b.iQuantity as iquantity','b.iNum as inum','b.iInvExchRate as iinvexchrate')
  379. ->get()->toArray();
  380. if(! empty($message)){
  381. foreach ($message as $key => $value){
  382. $message[$key] = (array)$value;
  383. }
  384. }
  385. return $message;
  386. }
  387. //产成品入库单单据号
  388. public function createOrderNumberAboutSCRK(){
  389. date_default_timezone_set("PRC");
  390. $current_month = date('m'); // 获取当前月份
  391. $date1 = date('y') . $current_month ; // 格式 "2307"
  392. $date2 = date('Y') . $current_month; // 格式 "202307"
  393. $record = $this->db->table('VoucherHistory')
  394. ->where('CardNumber','0411')
  395. ->whereRaw("(cSeed = '{$date1}' or cSeed = '{$date2}')")
  396. ->select('cNumber')
  397. ->first();
  398. $record = (array)$record;
  399. if(empty($record)) return [false,'未找到产成品入库当月流水号信息!'];
  400. $tmp = $record['cNumber'] + 1;
  401. if(strlen($tmp) > 4) return [false,'产成品入库当月流水号超过四位数!'];
  402. $number = str_pad($tmp,4,'0',STR_PAD_LEFT);
  403. $number = $tmp . $number;
  404. $ccode = "SCRK" . $date2 . $number;
  405. return [true,$ccode];
  406. }
  407. //销售出库单单据号
  408. public function createOrderNumberAboutXC(){
  409. date_default_timezone_set("PRC");
  410. $current_month = date('m'); // 获取当前月份
  411. $date1 = date('y') . $current_month ; // 格式 "2307"
  412. $date2 = date('Y') . $current_month; // 格式 "202307"
  413. $record = $this->db->table('VoucherHistory')
  414. ->where('CardNumber','0303')
  415. ->whereRaw("(cSeed = '{$date1}' or cSeed = '{$date2}')")
  416. ->select('cNumber')
  417. ->first();
  418. $record = (array)$record;
  419. if(empty($record)) return [false,'未找到销售出库单当月流水号信息!'];
  420. $tmp = $record['cNumber'] + 1;
  421. if(strlen($tmp) > 4) return [false,'销售出库单当月流水号超过四位数!'];
  422. $number = str_pad($tmp,4,'0',STR_PAD_LEFT);
  423. $number = $tmp . $number;
  424. $ccode = "XC" . $date2 . $number;
  425. return [true,$ccode];
  426. }
  427. public function post_helper($url, $data,$header = [],$timeout = 60){
  428. $ch = curl_init();
  429. curl_setopt($ch, CURLOPT_URL, $url);
  430. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  431. curl_setopt($ch, CURLOPT_ENCODING, '');
  432. curl_setopt($ch, CURLOPT_POST, 1);
  433. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  434. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  435. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  436. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  437. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  438. $r = curl_exec($ch);
  439. curl_close($ch);
  440. return json_decode($r, true);
  441. }
  442. }