U8ServerService.php 48 KB

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