U8ServerService.php 48 KB

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