U8ServerService.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DDEmployee;
  4. use App\Model\FieldData;
  5. use App\Model\Inventory;
  6. use App\Model\Record;
  7. use App\Model\Vendor;
  8. use Illuminate\Support\Facades\DB;
  9. class U8ServerService extends Service
  10. {
  11. protected $databaseService;
  12. /**
  13. * @var string|null
  14. */
  15. protected $error;
  16. /**
  17. * U8ServerService constructor.
  18. */
  19. public function __construct($loginUser = [])
  20. {
  21. $service = new U8DatabaseServerService($loginUser);
  22. $this->databaseService = $service->db;
  23. $this->error = $service->error;
  24. }
  25. /**
  26. * 获取错误信息
  27. *
  28. * @return string|null
  29. */
  30. public function getError()
  31. {
  32. return $this->error;
  33. }
  34. public function purchaseOrder($data, $user){
  35. $qx = $user['qx'];
  36. $order_date = $data['order_date'] ?? [];
  37. $order_date = array_filter($order_date);
  38. $order_number = $data['order_number'] ?? "";
  39. $model = $this->databaseService->table('PO_Pomain as a')
  40. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供应商
  41. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  42. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')// 业务员
  43. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  44. ->when(empty($qx), function ($query) use($user){
  45. return $query->where('a.cMaker', $user['username']);
  46. })
  47. ->when(!empty($order_number), function ($query) use($order_number){
  48. return $query->where('a.cPOID', 'LIKE', '%'.$order_number.'%');
  49. })
  50. ->when(!empty($order_date), function ($query) use($order_date){
  51. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  52. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  53. return $query->whereBetween('a.dPODate', [$start, $end]);
  54. })
  55. ->where(function ($query) {
  56. $query->where('a.iverifystateex', 0)
  57. ->orWhereNull('a.iverifystateex');
  58. })
  59. ->select(
  60. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  61. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"), // 订单日期
  62. DB::raw("ISNULL(a.cPOID, '') as order_number"), // 订单编号
  63. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  64. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供应商
  65. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  66. DB::raw("ISNULL(e.cPersonName, '') as person_name"), // 业务员
  67. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"), // 税率(数值转字符,null转0或空)
  68. DB::raw("ISNULL(a.cexch_name, '') as currency_name"), // 币种
  69. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"), // 汇率
  70. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  71. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  72. )
  73. ->orderBy('a.POID', 'desc');
  74. $list = $this->limit($model, '', $data);
  75. $list = $this->fillAll($list, 1, $user['login_type']);
  76. return [true, $list];
  77. }
  78. public function purchaseOrderDetail($data, $user){
  79. if(empty($data['order_number'])) return [false, '采购单号不能为空'];
  80. $order = $this->databaseService->table('PO_Pomain as a')
  81. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  82. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  83. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')
  84. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  85. ->where('a.cPOID', $data['order_number'])
  86. ->select(
  87. 'a.POID',
  88. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  89. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  90. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  91. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  92. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  93. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  94. DB::raw("ISNULL(e.cPersonName, '') as person_name"),
  95. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"),
  96. DB::raw("ISNULL(a.cexch_name, '') as currency_name"),
  97. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"),
  98. DB::raw("ISNULL(a.cMemo, '') as remark"),
  99. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  100. )
  101. ->first();
  102. if(empty($order)) return [false, '采购单不存在'];
  103. $order = (array) $order;
  104. // 获取明细
  105. $detail = $this->databaseService->table('PO_Podetails as a')
  106. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  107. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  108. ->where('a.POID', $order['POID'])
  109. ->select(
  110. DB::raw("ISNULL(b.cInvCode, '') as product_code"),
  111. DB::raw("ISNULL(b.cInvName, '') as product_title"),
  112. DB::raw("ISNULL(b.cInvStd, '') as product_std"),
  113. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"),
  114. // 数字转字符串并去除空格,NULL 则返回 '0.00' 或 ''
  115. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"),
  116. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 4)), '0.0000') as tax_unit_price"),
  117. DB::raw("ISNULL(LTRIM(STR(a.iUnitPrice, 20, 4)), '0.0000') as unit_price"),
  118. DB::raw("ISNULL(LTRIM(STR(a.iMoney, 20, 2)), '0.00') as amount"),
  119. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount"),
  120. DB::raw("ISNULL(LTRIM(STR(a.iPerTaxRate, 20, 2)), '0.00') as tax_rate"),
  121. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as arrive_date"),
  122. DB::raw("ISNULL(b.cEnterprise, '') as factory_name")
  123. )
  124. ->get();
  125. // 转为数组
  126. $order['detail'] = $detail->map(function ($item) {
  127. return (array) $item;
  128. })->toArray();
  129. // 移除内部 ID 避免暴露
  130. unset($order['POID']);
  131. return [true, $order];
  132. }
  133. public function purchaseRequisition($data, $user){
  134. $qx = $user['qx'];
  135. $order_date = $data['order_date'] ?? [];
  136. $order_date = array_filter($order_date);
  137. $order_number = $data['order_number'] ?? "";
  138. $model = $this->databaseService->table('PU_AppVouch as a')
  139. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode') // 请购人
  140. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 请购部门
  141. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  142. ->when(empty($qx), function ($query) use($user){
  143. return $query->where('a.cMaker',$user['username']);
  144. })
  145. ->when(! empty($order_number), function ($query) use($order_number){
  146. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  147. })
  148. ->when(! empty($order_date), function ($query) use($order_date){
  149. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  150. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  151. return $query->whereBetween('a.dDate', [$start, $end]);
  152. })
  153. ->where(function ($query) {
  154. $query->where('a.iverifystateex', 0)
  155. ->orWhereNull('a.iverifystateex');
  156. })
  157. ->select(
  158. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  159. DB::raw("ISNULL(a.cCode, '') as order_number"), // 单据号
  160. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 日期
  161. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 请购部门
  162. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"), // 请购人员
  163. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  164. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  165. )
  166. ->orderBy('a.ID','desc');
  167. $list = $this->limit($model,'',$data);
  168. $list = $this->fillAll($list, 2, $user['login_type']);
  169. return [true , $list];
  170. }
  171. public function purchaseRequisitionDetail($data, $user){
  172. if(empty($data['order_number'])) return [false, '采购请购单号不能为空'];
  173. $order = $this->databaseService->table('PU_AppVouch as a')
  174. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode')
  175. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  176. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  177. ->where('a.cCode', $data['order_number'])
  178. ->select(
  179. 'a.ID', // 用于关联子表
  180. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  181. DB::raw("ISNULL(a.cCode, '') as order_number"),
  182. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  183. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  184. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"),
  185. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  186. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  187. )
  188. ->first();
  189. if(empty($order)) return [false, '采购请购单不存在'];
  190. $order = (array) $order;
  191. $detail = $this->databaseService->table('PU_AppVouchs as a')
  192. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  193. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  194. ->where('a.ID', $order['ID'])
  195. ->select(
  196. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  197. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  198. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  199. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量
  200. DB::raw("ISNULL(LTRIM(STR(a.fQuantity, 20, 2)), '0.00') as quantity"), // 数量
  201. DB::raw("ISNULL(LTRIM(STR(a.fUnitPrice, 20, 4)), '0.0000') as unit_price"), // 本币单价
  202. DB::raw("ISNULL(LTRIM(STR(a.iOriSum, 20, 2)), '0.00') as tax_amount"), // 本币价税合计
  203. DB::raw("ISNULL(CONVERT(varchar(10), a.dRequirDate, 120), '') as need_arrived_date"), // 需求日期
  204. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as suggest_order_date"), // 建议订货日期
  205. DB::raw("ISNULL(b.cEnterprise, '') as factory_name") // 生产企业
  206. )
  207. ->get();
  208. // 转为数组格式
  209. $order['detail'] = $detail->map(function ($item) {
  210. return (array) $item;
  211. })->toArray();
  212. unset($order['ID']); // 隐藏内部ID
  213. return [true, $order];
  214. }
  215. public function purchaseInOrder($data, $user){
  216. $qx = $user['qx'];
  217. $order_date = $data['order_date'] ?? [];
  218. $order_date = array_filter($order_date);
  219. $order_number = $data['order_number'] ?? "";
  220. $model = $this->databaseService->table('RdRecord01 as a')
  221. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供货单位
  222. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode') // 仓库
  223. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  224. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')// 业务员
  225. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')// 采购类型
  226. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode') // 入库类别
  227. ->when(empty($qx), function ($query) use($user){
  228. return $query->where('a.cMaker',$user['username']);
  229. })
  230. ->when(! empty($order_number), function ($query) use($order_number){
  231. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  232. })
  233. ->when(! empty($order_date), function ($query) use($order_date){
  234. $start = date('Y-m-d 00:00:00.000', $order_date[0] / 1000);
  235. $end = date('Y-m-d 23:59:59.000', $order_date[1] / 1000);
  236. return $query->whereBetween('a.dDate', [$start, $end]);
  237. })
  238. ->whereNull('a.cHandler') // 未审核
  239. ->select(
  240. DB::raw("ISNULL(a.cCode, '') as order_number"), // 入库单号
  241. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 入库日期
  242. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"), // 仓库
  243. DB::raw("ISNULL(a.cOrderCode, '') as po_number"), // 订单号
  244. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"), // 到货单号
  245. DB::raw("ISNULL(p.cPersonName, '') as person_name"), // 业务员
  246. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供货单位
  247. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  248. DB::raw("ISNULL(CONVERT(varchar(10), a.dARVDate, 120), '') as arrival_date"), // 到货日期
  249. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  250. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"), // 采购类型
  251. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"), // 入库类别
  252. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"), // 审核日期
  253. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  254. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  255. )
  256. ->orderBy('a.ID','desc');
  257. $list = $this->limit($model,'',$data);
  258. $list = $this->fillAll($list, 3, $user['login_type']);
  259. return [true , $list];
  260. }
  261. public function purchaseInOrderDetail($data, $user){
  262. if(empty($data['order_number'])) return [false, '入库单号不能为空'];
  263. $order = $this->databaseService->table('RdRecord01 as a')
  264. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  265. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode')
  266. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  267. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')
  268. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')
  269. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode')
  270. ->where('a.cCode', $data['order_number'])
  271. ->select(
  272. 'a.ID',
  273. DB::raw("ISNULL(a.cCode, '') as order_number"),
  274. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  275. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"),
  276. DB::raw("ISNULL(a.cOrderCode, '') as po_number"),
  277. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"),
  278. DB::raw("ISNULL(p.cPersonName, '') as person_name"),
  279. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  280. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  281. DB::raw("ISNULL(CONVERT(varchar(10), a.darvdate, 120), '') as arrival_date"),
  282. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  283. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"),
  284. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"),
  285. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"),
  286. DB::raw("ISNULL(a.cMemo, '') as remark"),
  287. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  288. )
  289. ->first();
  290. if(empty($order)) return [false, '采购入库单不存在'];
  291. $order = (array) $order;
  292. $detail = $this->databaseService->table('rdrecords01 as a')
  293. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  294. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  295. ->where('a.ID', $order['ID'])
  296. ->select(
  297. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  298. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  299. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  300. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量单位
  301. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"), // 数量
  302. DB::raw("ISNULL(LTRIM(STR(a.iUnitCost, 20, 4)), '0.0000') as unit_price"), // 本币单价
  303. DB::raw("ISNULL(LTRIM(STR(a.iPrice, 20, 2)), '0.00') as amount"), // 本币金额
  304. DB::raw("ISNULL(LTRIM(STR(a.iTax, 20, 2)), '0.00') as tax"), // 税额
  305. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 2)), '0.00') as nat_tax"), // 本币税额 (入库单本币税额通常等于原币税额)
  306. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount") // 本币价税合计
  307. )
  308. ->get();
  309. $detailArr = $detail->map(function ($item) {
  310. return (array) $item;
  311. })->toArray();
  312. // 计算总金额(价税合计之和)
  313. $order['total_amount'] = number_format(array_sum(array_column($detailArr, 'tax_amount')), 2, '.', '');
  314. $order['detail'] = $detailArr;
  315. unset($order['ID']);
  316. return [true, $order];
  317. }
  318. public function inventoryDetail($data, $user){
  319. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  320. $order = Inventory::where('del_time',0)
  321. ->where('order_number', $data['order_number'])
  322. ->first();
  323. if(empty($order)) return [false, '存货不存在'];
  324. $order = (array) $order;
  325. return [true, $order];
  326. }
  327. public function vendorDetail($data, $user){
  328. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  329. $order = Vendor::where('del_time',0)
  330. ->where('order_number', $data['order_number'])
  331. ->first();
  332. if(empty($order)) return [false, '供应商不存在'];
  333. $order = (array) $order;
  334. return [true, $order];
  335. }
  336. public function getOrderDetails($data,$user){
  337. $type = $data['type'];
  338. if($type == 1){
  339. // 采购单
  340. [$success, $order] = $this->purchaseOrderDetail($data,$user);
  341. }elseif ($type == 2){
  342. // 采购请购单
  343. [$success, $order] = $this->purchaseRequisitionDetail($data,$user);
  344. }elseif ($type == 3){
  345. // 采购入库
  346. [$success, $order] = $this->purchaseInOrderDetail($data,$user);
  347. }elseif ($type == 4){
  348. // 存货
  349. [$success, $order] = $this->inventoryDetail($data,$user);
  350. }elseif ($type == 5){
  351. // 供应商
  352. [$success, $order] = $this->vendorDetail($data,$user);
  353. }
  354. return [$success, $order];
  355. }
  356. private function fillAll($list, $type, $login_type){
  357. if(empty($list['data'])) return $list;
  358. $map = Record::where('del_time',0)
  359. ->where('type', $type)
  360. ->where('login_type', $login_type)
  361. ->whereIn('order_number',array_column($list['data'],'order_number'))
  362. ->pluck('id','order_number')
  363. ->toArray();
  364. foreach ($list['data'] as $key => $value){
  365. if(isset($map[$value->order_number])) {
  366. $state = $value->state;
  367. $state_title = Record::state_name[$state];
  368. }else{
  369. $state = Record::state_minus_one;
  370. $state_title = Record::state_name[$state];
  371. }
  372. $list['data'][$key]->state = $state;
  373. $list['data'][$key]->state_title = $state_title;
  374. }
  375. return $list;
  376. }
  377. public function stockList($data, $user)
  378. {
  379. try {
  380. $field_list = [];
  381. $employee = DDEmployee::where('userid', $user['userid'])->where('login_type', $user['login_type'])->first();
  382. $qx = $employee['qx'] ?? 0;
  383. if(empty($qx)) {
  384. $field_list = FieldData::where('userid', $user['userid'])
  385. ->where('login_type', $user['login_type'])
  386. ->where('type', FieldData::STATE_ZERO)
  387. ->pluck('key')
  388. ->all();
  389. }
  390. // 1. 构建基础查询:关联现存量表和存货档案表
  391. $query = $this->databaseService->table('CurrentStock as S')
  392. ->select([
  393. 'S.cWhCode', // 仓库编码
  394. 'W.cWhName', // 仓库名称
  395. 'S.cInvCode', // 存货编码
  396. 'I.cInvName', // 存货名称
  397. 'I.cInvStd', // 规格型号
  398. 'I.cInvCCode', // 分类编码
  399. // --- 数量字段对齐你的结构 ---
  400. 'S.iQuantity', // 结存数量 (账面现存量)
  401. 'S.fAvaQuantity', // 可用数量
  402. 'S.fOutQuantity', // 待发货数量 (待出)
  403. 'S.fInQuantity', // 待入库数量 (待入)
  404. 'S.fStopQuantity', // 冻结数量
  405. // --- 批次与日期 ---
  406. 'S.cBatch', // 批号
  407. 'S.dMdate', // 生产日期
  408. 'S.dVDate', // 失效日期
  409. ])
  410. ->join('Inventory as I', 'S.cInvCode', '=', 'I.cInvCode')
  411. ->leftJoin('Warehouse as W', 'S.cWhCode', '=', 'W.cWhCode')
  412. ->leftJoin('InventoryClass as IC', 'I.cInvCCode', '=', 'IC.cInvCCode');
  413. // 2. 过滤条件:存货名称 (支持模糊查询)
  414. if (!empty($data['material_title'])) {
  415. $query->where('I.cInvName', 'like', '%' . $data['material_title'] . '%');
  416. }
  417. // 2. 过滤条件:存货编码 (支持模糊查询)
  418. if (!empty($data['material_code'])) {
  419. $query->where('S.cInvCode', 'like', '%' . $data['material_code'] . '%');
  420. }
  421. // 3. 过滤条件:存货分类 (支持左匹配,即选大类查出所有子类)
  422. if (!empty($data['category_code'])) {
  423. // U8 分类是级次结构,用 like '01%' 可以查出 01 开头的所有子类
  424. $query->where('I.cInvCCode', 'like', $data['category_code'] . '%');
  425. }
  426. // 6. 排序
  427. $query->orderBy('S.cInvCode', 'asc')->orderBy('S.cWhCode', 'asc');
  428. // 7. 调用你定义的分页方法
  429. $columns = ['*'];
  430. $result = $this->limit($query, $columns, $data);
  431. // 注意这里的 &$item,加了 & 符号才能直接修改原数组里的内容
  432. foreach ($result['data'] as &$item) {
  433. $numFields = ['iQuantity', 'fAvaQuantity', 'fOutQuantity', 'fInQuantity', 'fStopQuantity'];
  434. foreach ($numFields as $field) {
  435. if (isset($item->$field)) {
  436. $item->$field = (float)$item->$field;
  437. }
  438. }
  439. $item->dMdate = $item->dMdate ? date('Y-m-d', strtotime($item->dMdate)) : '';
  440. $item->dVDate = $item->dVDate ? date('Y-m-d', strtotime($item->dVDate)) : '';
  441. // 脱敏处理
  442. if (!empty($field_list)) {
  443. foreach ($field_list as $blackField) {
  444. if (isset($item->$blackField)) {
  445. $item->$blackField = '*****';
  446. }
  447. }
  448. }
  449. }
  450. unset($item); // 销毁引用
  451. return [true, $result];
  452. } catch (\Throwable $exception) {
  453. return [false, "查询库存失败: " . $exception->getMessage()];
  454. }
  455. }
  456. public function vendorU8List($data, $user)
  457. {
  458. // 1. 构建基础查询
  459. $query = $this->databaseService->table('Vendor as V')
  460. ->select([
  461. 'V.cVenCode', // 供应商编码
  462. 'V.cVenName', // 供应商名称
  463. 'V.cVenAbbName', // 供应商简称
  464. 'V.cVCCode', // 分类编码
  465. 'VC.cVCName', // 分类名称 (来自 VendorClass)
  466. 'V.cVenAddress', // 地址
  467. 'V.cVenPhone', // 电话
  468. 'V.dVenDevDate', // 发展日期
  469. 'V.cCreatePerson', // 创建人
  470. 'V.bVenTax', // 是否计税
  471. 'V.iId' // 内部ID
  472. ])
  473. // 关联供应商分类表获取分类名称
  474. ->leftJoin('VendorClass as VC', 'V.cVCCode', '=', 'VC.cVCCode');
  475. // 2. 增加搜索逻辑 (可选)
  476. if (!empty($data['keyword'])) {
  477. $keyword = $data['keyword'];
  478. $query->where(function($q) use ($keyword) {
  479. $q->where('V.cVenCode', 'like', "%{$keyword}%")
  480. ->orWhere('V.cVenName', 'like', "%{$keyword}%")
  481. ->orWhere('V.cVenAbbName', 'like', "%{$keyword}%");
  482. });
  483. }
  484. // 3. 排序 (默认按编码排序)
  485. $query->orderBy('V.cVenCode', 'ASC');
  486. // 4. 调用你定义的分页方法
  487. // 注意:limit 方法内部会执行 paginate 并将结果填充到 $data
  488. $columns = ['*']; // select 已经在上面定义过了,这里传 * 即可
  489. $result = $this->limit($query, $columns, $data);
  490. return [true, $result];
  491. }
  492. public function vendorClassTree($data, $user)
  493. {
  494. try {
  495. // 1. 获取所有供应商分类 (表名: VendorClass)
  496. $classes = $this->databaseService->table('VendorClass')
  497. ->select('cVCCode', 'cVCName', 'iVCGrade', 'bVCEnd') // 编码、名称、级次
  498. ->orderBy('cVCCode', 'asc')
  499. ->get();
  500. if ($classes->isEmpty()) {
  501. return [true, []];
  502. }
  503. // 2. 格式化数据,以编码为 Key
  504. $classList = [];
  505. foreach ($classes as $item) {
  506. $classList[$item->cVCCode] = [
  507. 'label' => $item->cVCName,
  508. 'value' => $item->cVCCode, // 前端通常需要 value 字段
  509. 'code' => $item->cVCCode,
  510. 'grade' => $item->iVCGrade,
  511. 'is_end' => $item->bVCEnd,
  512. 'children' => []
  513. ];
  514. }
  515. // 3. 构建引用树
  516. $tree = [];
  517. foreach ($classList as $code => &$node) {
  518. // 获取父级编码
  519. $parentCode = $this->getParentCode($code);
  520. if ($parentCode === null || !isset($classList[$parentCode])) {
  521. // 顶级节点
  522. $tree[] = &$node;
  523. } else {
  524. // 挂载到父节点
  525. $classList[$parentCode]['children'][] = &$node;
  526. }
  527. }
  528. return [true, $tree];
  529. } catch (\Throwable $exception) {
  530. return [false, "获取供应商分类树失败: " . $exception->getMessage()];
  531. }
  532. }
  533. /**
  534. * 辅助函数:根据 U8 编码规则获取父级编码
  535. * U8 的级次通常存储在 GradeDef 表,但通用逻辑是截取末尾
  536. */
  537. private function getParentCode($code)
  538. {
  539. $len = strlen($code);
  540. if ($len <= 2) return null; // 假设第一级是2位,小于等于2位则无父级
  541. // 这里假设级次是 2-2-2-2 (最常见配置)
  542. // 实际生产中,如果级次不固定,建议查询 GradeDef 表
  543. return substr($code, 0, $len - 2);
  544. }
  545. //U8 存货分类树结构
  546. public function inventoryClassTree($data, $user)
  547. {
  548. try {
  549. // 1. 从数据库获取所有存货分类
  550. $classes = $this->databaseService->table('InventoryClass')
  551. ->select('cInvCCode', 'cInvCName', 'iInvCGrade', 'bInvCEnd')
  552. ->orderBy('cInvCCode', 'asc')
  553. ->get();
  554. if ($classes->isEmpty()) return [true, []];
  555. // 2. 将集合转换为数组并以编码作为 Key,方便查找
  556. $classList = [];
  557. foreach ($classes as $item) {
  558. $classList[$item->cInvCCode] = [
  559. 'label' => $item->cInvCName,
  560. 'code' => $item->cInvCCode,
  561. 'grade' => $item->iInvCGrade,
  562. 'is_end' => $item->bInvCEnd,
  563. 'children' => []
  564. ];
  565. }
  566. // 3. 构建树形结构
  567. $tree = [];
  568. foreach ($classList as $code => &$node) {
  569. // 获取当前分类的级次 (U8 逻辑通常根据编码长度判断父级)
  570. // 比如 0101 的父级是 01
  571. $parentCode = $this->getParentCode($code);
  572. if ($parentCode === null || !isset($classList[$parentCode])) {
  573. // 如果没有父级编码,或者父级编码不在列表里,说明是顶级分类
  574. $tree[] = &$node;
  575. } else {
  576. // 将当前节点引用到父节点的 children 数组中
  577. $classList[$parentCode]['children'][] = &$node;
  578. }
  579. }
  580. return [true, $tree];
  581. } catch (\Throwable $exception) {
  582. return [false, "获取分类树失败: " . $exception->getMessage()];
  583. }
  584. }
  585. //U8 计量单位组(带默认主计量单位)
  586. public function getUnitGroups($data, $user)
  587. {
  588. $list = $this->databaseService->select("
  589. SELECT
  590. G.cGroupCode,
  591. G.cGroupName,
  592. G.iGroupType,
  593. U.cComUnitCode,
  594. U.cComUnitName,
  595. U.iNumber
  596. FROM ComputationGroup AS G
  597. OUTER APPLY (
  598. SELECT TOP 1 cComUnitCode, cComUnitName, iNumber
  599. FROM ComputationUnit
  600. WHERE cGroupCode = G.cGroupCode
  601. ORDER BY
  602. bMainUnit DESC, -- 1. 优先主计量
  603. iNumber ASC, -- 2. 序号最小 (若 NULL 会排在最前)
  604. cComUnitCode ASC -- 3. 编码最小
  605. ) AS U
  606. ");
  607. return [true, $list];
  608. }
  609. //U8 计量单位档案
  610. public function getComputationUnitList($data, $user)
  611. {
  612. $list = $this->databaseService->table('ComputationUnit as U')
  613. ->select(
  614. 'U.cComUnitCode', // 单位编码
  615. 'U.cComUnitName', // 单位名称
  616. 'U.cGroupCode', // 所属组编码
  617. 'U.bMainUnit', // 是否主单位
  618. 'U.iNumber' // 排序序号
  619. )
  620. ->orderBy('U.cGroupCode', 'ASC')
  621. ->orderBy('U.iNumber', 'ASC') // 按照你要求的 iNumber 排序
  622. ->get();
  623. return [true, $list];
  624. }
  625. //U8 存货新增到用友
  626. public function inventoryAddToU8($data, $user){
  627. $inventory = Inventory::where('del_time', 0)
  628. ->where('id', $data['id'])
  629. ->first();
  630. if(empty($inventory)) return [false, '存货记录不存在或已被删除'];
  631. $inventory = $inventory->toArray();
  632. if($inventory['status'] != Inventory::STATE_TWO) return [false, '存货记录未审核通过'];
  633. $title = $inventory['title'];
  634. $category_code = $inventory['category_code'];
  635. if(! empty($data['code'])){
  636. //用户传入
  637. $no = $inventory['code'];
  638. $flag_title = "重填";
  639. }else{
  640. //生成编码
  641. list($status, $msg) = $this->generate('inventory','1005');
  642. if(! $status) return [false, $msg];
  643. $no = $msg;
  644. $flag_title = "重试";
  645. }
  646. $inventoryData = [
  647. 'cInvCode' => $no,
  648. 'cInvName' => $title,
  649. 'cInvCCode' => $category_code,
  650. 'cInvStd' => $inventory['size'] ?? null, // 规格型号
  651. 'bSale' => $inventory['bSale'], //内销
  652. 'bExpSale' => $inventory['bExpSale'], //外销
  653. 'bPurchase' => $inventory['bPurchase'], // 采购
  654. 'bSelf' => $inventory['bSelf'], // 自制
  655. 'bComsume' => $inventory['bComsume'], // 生产耗材
  656. 'iGroupType' => $inventory['unit_group_type'], // 计量单位组类别
  657. 'cGroupCode' => $inventory['unit_group_code'], //计量单位组
  658. 'cComUnitCode' => $inventory['unit_code'], // 主计量单位
  659. 'cShopUnit' => $inventory['unit_code'], // 零售计量单位
  660. 'iImpTaxRate' => $inventory['iImpTaxRate'], // 进项税率
  661. 'iTaxRate' => $inventory['iTaxRate'], // 销项税率
  662. 'dSDate' => date("Y-m-d 00:00:00.000"), // 启用日期
  663. 'cEnterprise' => $inventory['vendor_code_title'] ?? null,
  664. 'iSupplyType' => '0', // 供应类型
  665. 'fConvertRate' => '1.0', // 转换因子
  666. 'bInTotalCost' => '1', // 成本累计否
  667. 'cPlanMethod' => 'R',
  668. 'cSRPolicy' => 'PE',
  669. 'bBomMain' => '0', // 允许BOM母件
  670. 'bBomSub' => '0', // 允许BOM子件
  671. 'bProductBill' => '0', // 允许生产订单
  672. 'iPlanDefault' => '1',
  673. 'iAllocatePrintDgt' => '4',
  674. 'bService' => '0',
  675. 'bAccessary' => '0',
  676. 'iInvAdvance' => '0.0',
  677. 'bInvQuality' => '0',
  678. 'bInvBatch' => '0',
  679. 'bInvEntrust' => '0',
  680. 'bInvOverStock' => '0',
  681. 'bFree1' => '0',
  682. 'bFree2' => '0',
  683. 'bInvType' => '0',
  684. 'bFree3' => '0',
  685. 'bFree4' => '0',
  686. 'bFree5' => '0',
  687. 'bFree6' => '0',
  688. 'bFree7' => '0',
  689. 'bFree8' => '0',
  690. 'bFree9' => '0',
  691. 'bFree10' => '0',
  692. 'cCreatePerson' => 'demo',
  693. 'cModifyPerson' => 'demo', // 变更
  694. 'dModifyDate' => date("Y-m-d H:i:s.000"),
  695. 'bFixExch' => '0',
  696. 'bTrack' => '0',
  697. 'bSerial' => '0',
  698. 'bBarCode' => '0',
  699. 'bSolitude' => '0',
  700. 'bSpecialties' => '0',
  701. 'bPropertyCheck' => '0',
  702. 'iRecipeBatch' => '0',
  703. 'bPromotSales' => '0',
  704. 'bPlanInv' => '0',
  705. 'bProxyForeign' => '0',
  706. 'bATOModel' => '0',
  707. 'bCheckItem' => '0',
  708. 'bPTOModel' => '0',
  709. 'bEquipment' => '0',
  710. 'bMPS' => '0',
  711. 'bROP' => '0',
  712. 'bRePlan' => '0',
  713. 'bBillUnite' => '0',
  714. 'bCutMantissa' => '0',
  715. 'bConfigFree1' => '0',
  716. 'bConfigFree2' => '0',
  717. 'bConfigFree3' => '0',
  718. 'bConfigFree4' => '0',
  719. 'bConfigFree5' => '0',
  720. 'bConfigFree6' => '0',
  721. 'bConfigFree7' => '0',
  722. 'bConfigFree8' => '0',
  723. 'bConfigFree9' => '0',
  724. 'bConfigFree10' => '0',
  725. 'bPeriodDT' => '0',
  726. 'bOutInvDT' => '0',
  727. 'bBackInvDT' => '0',
  728. 'bDTWarnInv' => '0',
  729. 'bImportMedicine' => '0',
  730. 'bFirstBusiMedicine' => '0',
  731. 'bForeExpland' => '0',
  732. 'bInvModel' => '0',
  733. 'bKCCutMantissa' => '0',
  734. 'bReceiptByDT' => '0',
  735. 'bCheckBSATP' => '0',
  736. 'bCheckFree1' => '0',
  737. 'bCheckFree2' => '0',
  738. 'bCheckFree3' => '0',
  739. 'bCheckFree4' => '0',
  740. 'bCheckFree5' => '0',
  741. 'bCheckFree6' => '0',
  742. 'bCheckFree7' => '0',
  743. 'bCheckFree8' => '0',
  744. 'bCheckFree9' => '0',
  745. 'bCheckFree10' => '0',
  746. 'iCheckATP' => '0',
  747. 'bPiece' => '0',
  748. 'bSrvItem' => '0',
  749. 'bSrvFittings' => '0',
  750. 'bSpecialOrder' => '0',
  751. 'bTrackSaleBill' => '0',
  752. 'bCheckBatch' => '0',
  753. 'bMngOldpart' => '0',
  754. ];
  755. $inventorySubData = [
  756. 'cInvSubCode' => $no,
  757. 'iSurenessType' => '1',
  758. 'bIsAttachFile' => '0',
  759. 'bInByProCheck' => '1',
  760. 'iRequireTrackStyle' => '0',
  761. 'iExpiratDateCalcu' => '0',
  762. 'iBOMExpandUnitType' => '1',
  763. 'iDrawType' => $inventory['iDrawType'] ?? 0, // 领用方式
  764. 'fInvCIQExch' => $inventory['customs_change_rate'] ?? 1, // 海关换算率
  765. 'bInvKeyPart' => '1',
  766. 'iAcceptEarlyDays' => '999',
  767. 'dInvCreateDatetime' => date("Y-m-d H:i:s.000"),
  768. 'bPUQuota' => '0',
  769. 'bInvROHS' => '0',
  770. 'bPrjMat' => '0',
  771. 'bInvAsset' => '0',
  772. 'bSrvProduct' => '0',
  773. 'iAcceptDelayDays' => '0',
  774. 'bSCkeyProjections' => '0',
  775. 'iSupplyPeriodType' => '1',
  776. 'iAvailabilityDate' => '1',
  777. 'bImport' => '0',
  778. 'bCheckSubitemCost' => '1',
  779. 'fRoundFactor' => '0.0',
  780. 'bConsiderFreeStock' => '1',
  781. 'bSuitRetail' => '0',
  782. 'bFeatureMatch' => '0',
  783. 'bProduceByFeatureAllocate' => '0',
  784. 'bMaintenance' => '0',
  785. 'iMaintenanceCycleUnit' => '0',
  786. 'bCoupon' => '0',
  787. 'bStoreCard' => '0',
  788. 'bProcessProduct' => '0',
  789. 'bProcessMaterial' => '0',
  790. // 所有的价格自由项默认设为 '0'
  791. 'bPurPriceFree1' => '0', 'bPurPriceFree2' => '0', 'bPurPriceFree3' => '0', 'bPurPriceFree4' => '0', 'bPurPriceFree5' => '0',
  792. 'bPurPriceFree6' => '0', 'bPurPriceFree7' => '0', 'bPurPriceFree8' => '0', 'bPurPriceFree9' => '0', 'bPurPriceFree10' => '0',
  793. 'bOMPriceFree1' => '0', 'bOMPriceFree2' => '0', 'bOMPriceFree3' => '0', 'bOMPriceFree4' => '0', 'bOMPriceFree5' => '0',
  794. 'bOMPriceFree6' => '0', 'bOMPriceFree7' => '0', 'bOMPriceFree8' => '0', 'bOMPriceFree9' => '0', 'bOMPriceFree10' => '0',
  795. 'bSalePriceFree1' => '0', 'bSalePriceFree2' => '0', 'bSalePriceFree3' => '0', 'bSalePriceFree4' => '0', 'bSalePriceFree5' => '0',
  796. 'bSalePriceFree6' => '0', 'bSalePriceFree7' => '0', 'bSalePriceFree8' => '0', 'bSalePriceFree9' => '0', 'bSalePriceFree10' => '0',
  797. // 所有的控制自由项默认设为 '0'
  798. 'bControlFreeRange1' => '0', 'bControlFreeRange2' => '0', 'bControlFreeRange3' => '0', 'bControlFreeRange4' => '0', 'bControlFreeRange5' => '0',
  799. 'bControlFreeRange6' => '0', 'bControlFreeRange7' => '0', 'bControlFreeRange8' => '0', 'bControlFreeRange9' => '0', 'bControlFreeRange10' => '0',
  800. // 所有的批次属性默认设为 '0'
  801. 'bBatchProperty1' => '0', 'bBatchProperty2' => '0', 'bBatchProperty3' => '0', 'bBatchProperty4' => '0', 'bBatchProperty5' => '0',
  802. 'bBatchProperty6' => '0', 'bBatchProperty7' => '0', 'bBatchProperty8' => '0', 'bBatchProperty9' => '0', 'bBatchProperty10' => '0',
  803. 'bBondedInv' => '0',
  804. 'bBatchCreate' => '0',
  805. ];
  806. try {
  807. $this->databaseService->beginTransaction();
  808. $exists = $this->databaseService->table('Inventory')
  809. ->where('cInvCode', $inventoryData['cInvCode'])
  810. ->lockForUpdate() // 锁定该行,防止并发冲突
  811. ->exists();
  812. if ($exists) return [false, '存货编码已存在,请' . $flag_title];
  813. $this->databaseService->table('Inventory')->insert($inventoryData);
  814. $this->databaseService->table('Inventory_sub')->insert($inventorySubData);
  815. $this->databaseService->commit();
  816. } catch (\Throwable $exception) {
  817. $this->databaseService->rollBack();
  818. return [false, "创建用友失败: " . $exception->getMessage()];
  819. }
  820. return [true, ''];
  821. }
  822. //U8 供应商新增
  823. public function vendorAddToU8($data, $user){
  824. $vendor = Vendor::where('del_time', 0)
  825. ->where('id', $data['id'])
  826. ->first();
  827. if(empty($vendor)) return [false, '供应商记录不存在或已被删除'];
  828. $vendor = $vendor->toArray();
  829. if($vendor['status'] != Vendor::STATE_TWO) return [false, '供应商记录未审核通过'];
  830. if(! empty($vendor['code'])){
  831. //用户传入
  832. $no = $vendor['code'];
  833. $flag_title = "重填";
  834. }else{
  835. //生成编码
  836. list($status, $msg) = $this->generate('vendor');
  837. if(! $status) return [false, $msg];
  838. $no = $msg;
  839. $flag_title = "重试";
  840. }
  841. $vendorData = [
  842. 'cVenCode' => $no,
  843. 'cVenName' => $vendor['title'],
  844. 'cVenAbbName' => $vendor['easy_title'],
  845. 'cVCCode' => $vendor['category_code'],
  846. 'dVenDevDate' => date("Y-m-d 00:00:00.000"),
  847. 'iVenDisRate' => '0.0',
  848. 'iVenCreLine' => '0.0',
  849. 'iVenCreDate' => '0',
  850. 'cVenHeadCode' => $no,
  851. 'iAPMoney' => '0.0',
  852. 'iLastMoney' => '0.0',
  853. 'iLRMoney' => '0.0',
  854. 'iFrequency' => '0',
  855. 'bVenTax' => '1',
  856. 'cCreatePerson' => 'demo',
  857. 'cModifyPerson' => 'demo',
  858. 'dModifyDate' => date("Y-m-d H:i:s.000"),
  859. 'iGradeABC' => '-1',
  860. 'bLicenceDate' => '0',
  861. 'bBusinessDate' => '0',
  862. 'bProxyDate' => '0',
  863. 'bPassGMP' => '0',
  864. 'bVenCargo' => '1', // 采购
  865. 'bProxyForeign' => '0', // 委外
  866. 'bVenService' => '0', // 服务
  867. 'bVenOverseas' => '0', // 国外
  868. 'cVenExch_name' => '人民币',
  869. 'iVenGSPType' => '0',
  870. 'iVenGSPAuth' => '-1',
  871. 'bVenAccPeriodMng' => '0',
  872. 'bVenHomeBranch' => '0',
  873. 'dVenCreateDatetime' => date("Y-m-d H:i:s.000"),
  874. 'bIsVenAttachFile' => '0',
  875. 'bRetail' => '0',
  876. ];
  877. try {
  878. $this->databaseService->beginTransaction();
  879. $exists = $this->databaseService->table('Vendor')
  880. ->where('cVenCode', $vendorData['cVenCode'])
  881. ->lockForUpdate() // 锁定该行,防止并发冲突
  882. ->exists();
  883. if ($exists) return [false, '供应商编码已存在,请' . $flag_title];
  884. $this->databaseService->table('Vendor')->insert($vendorData);
  885. $this->databaseService->commit();
  886. } catch (\Throwable $exception) {
  887. $this->databaseService->rollBack();
  888. return [false, "创建供应商失败: " . $exception->getMessage()];
  889. }
  890. return [true, ''];
  891. }
  892. // 生成编码
  893. public function generate($type, $classCode = "")
  894. {
  895. try {
  896. // 1. 确定对象映射
  897. $cardNumber = ($type === 'inventory') ? 'inventory' : 'Vendor';
  898. $cardNumber_name = ($type === 'inventory') ? '存货' : '供应商';
  899. $table = ($type === 'inventory') ? 'Inventory' : 'Vendor';
  900. $codeField = ($type === 'inventory') ? 'cInvCode' : 'cVenCode';
  901. // 2. 获取 U8 规则定义
  902. $rule = $this->databaseService->table('VoucherNumber')
  903. ->where('CardNumber', $cardNumber)
  904. ->first();
  905. if (!$rule) return [false, "未找到 {$cardNumber_name} 的规则定义"];
  906. /**
  907. * 3. 动态解析前缀 (Prefix1, Prefix2, Prefix3)
  908. * U8 的规则通常存放在 PrefixRule 或 Prefix 字段中
  909. */
  910. $prefix = "";
  911. for ($i = 1; $i <= 3; $i++) {
  912. $ruleField = "Prefix{$i}Rule";
  913. $valField = "Prefix{$i}";
  914. // 检查规则描述
  915. $ruleDesc = $rule->$ruleField ?? ''; // 例如 "存货分类编码" 或 "GYS"
  916. $staticVal = $rule->$valField ?? '';
  917. if (str_contains($ruleDesc, '分类') || str_contains($staticVal, '分类')) {
  918. // 如果规则提到“分类”,则填入传入的分类编码
  919. $prefix .= $classCode;
  920. } elseif (!empty($ruleDesc)) {
  921. // 如果规则是具体的字符(如 "GYS"),直接拼接
  922. $prefix .= $ruleDesc;
  923. } elseif (!empty($staticVal) && $staticVal !== '手工输入' && $staticVal !== '存货分类编码') {
  924. // 如果静态值不是提示语,则作为前缀
  925. $prefix .= $staticVal;
  926. }
  927. }
  928. // 4. 获取流水号配置 (Glide)
  929. $glideLen = $rule->GlideLen > 0 ? $rule->GlideLen : 4;
  930. $startNum = $rule->iStartNumber ?? 1;
  931. // 5. 查找当前最大编码
  932. $expectedLen = strlen($prefix) + $glideLen;
  933. $lastCode = $this->databaseService->table($table)
  934. ->where($codeField, 'like', $prefix . '%')
  935. ->whereRaw("LEN($codeField) = $expectedLen")
  936. ->orderBy($codeField, 'desc')
  937. ->value($codeField);
  938. // 6. 生成编码
  939. if (!$lastCode) {
  940. // 初始:前缀 + 起始值补零
  941. $finalCode = $prefix . str_pad($startNum, $glideLen, '0', STR_PAD_LEFT);
  942. } else {
  943. // 截取流水号自增
  944. $lastSerial = substr($lastCode, -$glideLen);
  945. $nextSerial = ++$lastSerial;
  946. // 溢出检查
  947. if (strlen($nextSerial) > $glideLen) {
  948. return [false, "流水号已溢出"];
  949. }
  950. $finalCode = $prefix . str_pad($nextSerial, $glideLen, '0', STR_PAD_LEFT);
  951. }
  952. return [true, $finalCode];
  953. } catch (\Throwable $e) {
  954. return [false, "生成失败: " . $e->getMessage()];
  955. }
  956. }
  957. }