FyySqlServerService.php 25 KB

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