FyySqlServerService.php 29 KB

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