U8ServerService.php 48 KB

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